Skip to content

Commit b3019e1

Browse files
Merge pull request #26 from OneFineStarstuff/penify/auto_doc_ea03621_86c59
[Penify]: Documentation for commit - ea03621
2 parents ea03621 + 5e8d684 commit b3019e1

5 files changed

Lines changed: 44 additions & 89 deletions

File tree

agi-pipeline.py

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ def __init__(self, model_name="facebook/bart-large-cnn"):
5858
) # nosec B615
5959

6060
def process_text(self, text, max_length=25, num_beams=5):
61-
"""
62-
Method process_text.
63-
"""
61+
"""Process and summarize the given text using a model."""
6462
logging.info("Processing text for summarization")
6563
try:
6664
inputs = self.tokenizer(
@@ -119,9 +117,7 @@ def preprocess_large_image(image_path, max_size=(2000, 2000)):
119117
return None
120118

121119
def process_image(self, image_path):
122-
"""
123-
Method process_image.
124-
"""
120+
"""Process an image for classification."""
125121
logging.info("Processing image for classification")
126122
try:
127123
image_path = self.preprocess_large_image(
@@ -156,9 +152,7 @@ def __init__(self):
156152
)
157153

158154
def process_image(self, image_path):
159-
"""
160-
Method process_image.
161-
"""
155+
"""Process an image for classification with augmentation."""
162156
logging.info("Processing image with augmentation for classification")
163157
try:
164158
image_path = self.preprocess_large_image(
@@ -227,16 +221,12 @@ def __init__(self):
227221
self.state = 50
228222

229223
def reset(self):
230-
"""
231-
Method reset.
232-
"""
224+
"""Resets the state to 50 and returns it as a numpy array."""
233225
self.state = 50
234226
return np.array([self.state], dtype=np.float32)
235227

236228
def step(self, action):
237-
"""
238-
Method step.
239-
"""
229+
"""Executes a step in the environment based on the given action."""
240230
reward = -abs(self.state - (50 + action * 10))
241231
self.state += action - 2
242232
done = self.state <= 0 or self.state >= 100
@@ -256,9 +246,7 @@ def __init__(self):
256246
self.model = PPO("MlpPolicy", self.env, verbose=1)
257247

258248
def train(self, timesteps=10000):
259-
"""
260-
Method train.
261-
"""
249+
"""Trains the RL model for a specified number of timesteps."""
262250
logging.info("Training RL model")
263251
try:
264252
self.model.learn(total_timesteps=timesteps)
@@ -267,9 +255,7 @@ def train(self, timesteps=10000):
267255
logging.error(f"Error in RLModule training: {e}")
268256

269257
def save_model(self, path):
270-
"""
271-
Method save_model.
272-
"""
258+
"""Saves the model to the specified path."""
273259
try:
274260
self.model.save(path)
275261
logging.info(f"Model saved to {path}")
@@ -341,9 +327,7 @@ def extract_frames(
341327
return frame_count
342328

343329
def process_frame(self, frame_path):
344-
"""
345-
Method process_frame.
346-
"""
330+
"""Processes an image frame and returns a tensor."""
347331
try:
348332
image = Image.open(frame_path).convert("RGB")
349333
tensor = self.transform(image).unsqueeze(0)
@@ -365,8 +349,17 @@ def __init__(self):
365349
super().__init__()
366350

367351
def process_real_time_video(self, source=0):
368-
"""
369-
Method process_real_time_video.
352+
"""Process real-time video from a specified source.
353+
354+
This method captures video from the given source and processes each frame in
355+
real-time. It checks if the video source is opened successfully, and if not,
356+
logs an error. The frames are resized and transformed before being displayed
357+
in a window. The processing continues until the video ends or the user presses
358+
the 'q' key to quit. Finally, it releases the video capture and closes all
359+
OpenCV windows.
360+
361+
Args:
362+
source (int or str): The video source, which can be an integer for
370363
"""
371364
cap = cv2.VideoCapture(source)
372365
if not cap.isOpened():
@@ -401,9 +394,7 @@ def __init__(self):
401394
self.engine = pyttsx3.init()
402395

403396
def speech_to_text(self, audio_file):
404-
"""
405-
Method speech_to_text.
406-
"""
397+
"""Converts speech from an audio file to text."""
407398
try:
408399
with sr.AudioFile(audio_file) as source:
409400
audio = self.recognizer.record(source)
@@ -443,9 +434,7 @@ def __init__(self):
443434
self.voice_processor = VoiceProcessor()
444435

445436
def process_input(self, text=None, image_path=None):
446-
"""
447-
Method process_input.
448-
"""
437+
"""Processes text and image input and returns the results."""
449438
results = {}
450439
if text:
451440
results["nlp"] = self.nlp.process_text(text)
@@ -454,25 +443,19 @@ def process_input(self, text=None, image_path=None):
454443
return results
455444

456445
def process_multi_modal(self, text, image_path):
457-
"""
458-
Method process_multi_modal.
459-
"""
446+
"""Processes text and image using multi-modal processing."""
460447
return self.multi_modal.process_text_image(text, image_path)
461448

462449
def process_video(self, video_path, frame_output_dir):
463-
"""
464-
Method process_video.
465-
"""
450+
"""Process a video and extract its frames."""
466451
frame_count = self.video_processor.extract_frames(video_path, frame_output_dir)
467452
if frame_count == 0:
468453
logging.error("No frames were saved. Please check the video file and path.")
469454
return
470455
logging.info(f"Video frames processed and saved to {frame_output_dir}")
471456

472457
def process_real_time_video(self, source=0):
473-
"""
474-
Method process_real_time_video.
475-
"""
458+
"""Processes real-time video from the specified source."""
476459
self.real_time_video_processor.process_real_time_video(source)
477460

478461
def train_rl(self, timesteps=10000):
@@ -482,15 +465,11 @@ def train_rl(self, timesteps=10000):
482465
self.rl.train(timesteps)
483466

484467
def choose_action(self, state):
485-
"""
486-
Method choose_action.
487-
"""
468+
"""Selects an action based on the given state."""
488469
return self.rl.choose_action(state)
489470

490471
def visualize_data(self, data):
491-
"""
492-
Method visualize_data.
493-
"""
472+
"""Visualizes the given data using a bar chart."""
494473
try:
495474
fig = px.bar(
496475
x=list(data.keys()), y=list(data.values()), title="Data Visualization"
@@ -500,15 +479,11 @@ def visualize_data(self, data):
500479
logging.error(f"Error in data visualization: {e}")
501480

502481
def speech_to_text(self, audio_file):
503-
"""
504-
Method speech_to_text.
505-
"""
482+
"""Converts speech from an audio file to text."""
506483
return self.voice_processor.speech_to_text(audio_file)
507484

508485
def text_to_speech(self, text):
509-
"""
510-
Method text_to_speech.
511-
"""
486+
"""Converts text to speech using the voice processor."""
512487
self.voice_processor.text_to_speech(text)
513488

514489

cv_module.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ def __init__(self):
2222
logger.info("CV model loaded successfully.")
2323

2424
def detect_objects(self, image: Image.Image) -> str:
25-
"""
26-
Detects objects in the provided image.
27-
25+
"""Detects objects in the provided image.
26+
2827
Args:
2928
image (Image.Image): The input image.
30-
29+
3130
Returns:
3231
str: JSON string containing detection results.
33-
32+
3433
Raises:
3534
ValueError: If the image is None.
3635
"""

fix_agi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
def add_docstrings(content):
55
# Add module docstring if missing
6+
"""Add missing module and class/method/function docstrings to the content."""
67
if not content.startswith('"""'):
78
content = '"""\nAGI Pipeline Legacy Module.\n"""\n' + content
89

main.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,15 @@ def __init__(self):
3333
self.speech = SpeechProcessor()
3434

3535
def process_nlp(self, prompt: str) -> str:
36-
"""
37-
Processes text using the NLP module.
38-
"""
36+
"""Processes text using the NLP module."""
3937
return self.nlp.generate_text(prompt)
4038

4139
def process_cv(self, image: Image.Image) -> str:
42-
"""
43-
Processes an image using the CV module.
44-
"""
40+
"""Processes an image to detect objects using the CV module."""
4541
return self.cv.detect_objects(image)
4642

4743
def process_stt(self, file: UploadFile) -> str:
48-
"""
49-
Processes audio using the STT module.
50-
"""
44+
"""Processes audio and converts it to text using the STT module."""
5145
return self.speech.speech_to_text(file)
5246

5347
def process_tts(self, text: str) -> None:
@@ -62,18 +56,14 @@ def process_tts(self, text: str) -> None:
6256

6357

6458
def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
65-
"""
66-
Verifies the Bearer token in the Authorization header.
67-
"""
59+
"""Verifies the Bearer token in the Authorization header."""
6860
if credentials.credentials != VALID_API_KEY:
6961
raise HTTPException(status_code=403, detail="Forbidden")
7062

7163

7264
@app.post("/process-nlp/", dependencies=[Depends(verify_token)])
7365
async def process_nlp(data: dict):
74-
"""
75-
Endpoint for text generation.
76-
"""
66+
"""Processes natural language input and generates a response."""
7767
try:
7868
prompt = data.get("text", "")
7969
return {"response": agi.process_nlp(prompt)}
@@ -84,9 +74,7 @@ async def process_nlp(data: dict):
8474

8575
@app.post("/process-cv-detection/", dependencies=[Depends(verify_token)])
8676
async def process_cv_detection(file: UploadFile = File(...)):
87-
"""
88-
Endpoint for object detection in images.
89-
"""
77+
"""Handles object detection in uploaded images."""
9078
try:
9179
image_data = await file.read()
9280
image = Image.open(BytesIO(image_data))
@@ -98,9 +86,7 @@ async def process_cv_detection(file: UploadFile = File(...)):
9886

9987
@app.post("/speech-to-text/", dependencies=[Depends(verify_token)])
10088
async def speech_to_text(file: UploadFile = File(...)):
101-
"""
102-
Endpoint for Speech-to-Text conversion.
103-
"""
89+
"""Converts speech from an uploaded file to text."""
10490
try:
10591
return {"response": agi.process_stt(file)}
10692
except Exception as e:
@@ -110,9 +96,7 @@ async def speech_to_text(file: UploadFile = File(...)):
11096

11197
@app.post("/text-to-speech/", dependencies=[Depends(verify_token)])
11298
async def text_to_speech(data: dict):
113-
"""
114-
Endpoint for Text-to-Speech conversion.
115-
"""
99+
"""Converts text to speech."""
116100
try:
117101
text = data.get("text", "")
118102
agi.process_tts(text)

nlp_module.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,11 @@ def __init__(self):
2727
logger.info("NLP model loaded successfully.")
2828

2929
def generate_text(self, prompt: str) -> str:
30-
"""
31-
Generates text based on the provided prompt.
32-
30+
"""Generates text based on the provided prompt.
31+
3332
Args:
3433
prompt (str): The input text to process.
35-
36-
Returns:
37-
str: The generated response.
38-
34+
3935
Raises:
4036
ValueError: If the prompt is empty.
4137
"""

0 commit comments

Comments
 (0)