diff --git a/agi-pipeline.py b/agi-pipeline.py index f669a6b..68bc367 100644 --- a/agi-pipeline.py +++ b/agi-pipeline.py @@ -58,9 +58,7 @@ def __init__(self, model_name="facebook/bart-large-cnn"): ) # nosec B615 def process_text(self, text, max_length=25, num_beams=5): - """ - Method process_text. - """ + """Process and summarize the given text using a model.""" logging.info("Processing text for summarization") try: inputs = self.tokenizer( @@ -119,9 +117,7 @@ def preprocess_large_image(image_path, max_size=(2000, 2000)): return None def process_image(self, image_path): - """ - Method process_image. - """ + """Process an image for classification.""" logging.info("Processing image for classification") try: image_path = self.preprocess_large_image( @@ -156,9 +152,7 @@ def __init__(self): ) def process_image(self, image_path): - """ - Method process_image. - """ + """Process an image for classification with augmentation.""" logging.info("Processing image with augmentation for classification") try: image_path = self.preprocess_large_image( @@ -227,16 +221,12 @@ def __init__(self): self.state = 50 def reset(self): - """ - Method reset. - """ + """Resets the state to 50 and returns it as a numpy array.""" self.state = 50 return np.array([self.state], dtype=np.float32) def step(self, action): - """ - Method step. - """ + """Executes a step in the environment based on the given action.""" reward = -abs(self.state - (50 + action * 10)) self.state += action - 2 done = self.state <= 0 or self.state >= 100 @@ -256,9 +246,7 @@ def __init__(self): self.model = PPO("MlpPolicy", self.env, verbose=1) def train(self, timesteps=10000): - """ - Method train. - """ + """Trains the RL model for a specified number of timesteps.""" logging.info("Training RL model") try: self.model.learn(total_timesteps=timesteps) @@ -267,9 +255,7 @@ def train(self, timesteps=10000): logging.error(f"Error in RLModule training: {e}") def save_model(self, path): - """ - Method save_model. - """ + """Saves the model to the specified path.""" try: self.model.save(path) logging.info(f"Model saved to {path}") @@ -341,9 +327,7 @@ def extract_frames( return frame_count def process_frame(self, frame_path): - """ - Method process_frame. - """ + """Processes an image frame and returns a tensor.""" try: image = Image.open(frame_path).convert("RGB") tensor = self.transform(image).unsqueeze(0) @@ -365,8 +349,17 @@ def __init__(self): super().__init__() def process_real_time_video(self, source=0): - """ - Method process_real_time_video. + """Process real-time video from a specified source. + + This method captures video from the given source and processes each frame in + real-time. It checks if the video source is opened successfully, and if not, + logs an error. The frames are resized and transformed before being displayed + in a window. The processing continues until the video ends or the user presses + the 'q' key to quit. Finally, it releases the video capture and closes all + OpenCV windows. + + Args: + source (int or str): The video source, which can be an integer for """ cap = cv2.VideoCapture(source) if not cap.isOpened(): @@ -401,9 +394,7 @@ def __init__(self): self.engine = pyttsx3.init() def speech_to_text(self, audio_file): - """ - Method speech_to_text. - """ + """Converts speech from an audio file to text.""" try: with sr.AudioFile(audio_file) as source: audio = self.recognizer.record(source) @@ -443,9 +434,7 @@ def __init__(self): self.voice_processor = VoiceProcessor() def process_input(self, text=None, image_path=None): - """ - Method process_input. - """ + """Processes text and image input and returns the results.""" results = {} if text: results["nlp"] = self.nlp.process_text(text) @@ -454,15 +443,11 @@ def process_input(self, text=None, image_path=None): return results def process_multi_modal(self, text, image_path): - """ - Method process_multi_modal. - """ + """Processes text and image using multi-modal processing.""" return self.multi_modal.process_text_image(text, image_path) def process_video(self, video_path, frame_output_dir): - """ - Method process_video. - """ + """Process a video and extract its frames.""" frame_count = self.video_processor.extract_frames(video_path, frame_output_dir) if frame_count == 0: logging.error("No frames were saved. Please check the video file and path.") @@ -470,9 +455,7 @@ def process_video(self, video_path, frame_output_dir): logging.info(f"Video frames processed and saved to {frame_output_dir}") def process_real_time_video(self, source=0): - """ - Method process_real_time_video. - """ + """Processes real-time video from the specified source.""" self.real_time_video_processor.process_real_time_video(source) def train_rl(self, timesteps=10000): @@ -482,15 +465,11 @@ def train_rl(self, timesteps=10000): self.rl.train(timesteps) def choose_action(self, state): - """ - Method choose_action. - """ + """Selects an action based on the given state.""" return self.rl.choose_action(state) def visualize_data(self, data): - """ - Method visualize_data. - """ + """Visualizes the given data using a bar chart.""" try: fig = px.bar( x=list(data.keys()), y=list(data.values()), title="Data Visualization" @@ -500,15 +479,11 @@ def visualize_data(self, data): logging.error(f"Error in data visualization: {e}") def speech_to_text(self, audio_file): - """ - Method speech_to_text. - """ + """Converts speech from an audio file to text.""" return self.voice_processor.speech_to_text(audio_file) def text_to_speech(self, text): - """ - Method text_to_speech. - """ + """Converts text to speech using the voice processor.""" self.voice_processor.text_to_speech(text) diff --git a/cv_module.py b/cv_module.py index 5126efd..f7842d8 100644 --- a/cv_module.py +++ b/cv_module.py @@ -22,15 +22,14 @@ def __init__(self): logger.info("CV model loaded successfully.") def detect_objects(self, image: Image.Image) -> str: - """ - Detects objects in the provided image. - + """Detects objects in the provided image. + Args: image (Image.Image): The input image. - + Returns: str: JSON string containing detection results. - + Raises: ValueError: If the image is None. """ diff --git a/fix_agi.py b/fix_agi.py index 7674184..cded220 100644 --- a/fix_agi.py +++ b/fix_agi.py @@ -3,6 +3,7 @@ def add_docstrings(content): # Add module docstring if missing + """Add missing module and class/method/function docstrings to the content.""" if not content.startswith('"""'): content = '"""\nAGI Pipeline Legacy Module.\n"""\n' + content diff --git a/main.py b/main.py index 429dcbe..db7b059 100644 --- a/main.py +++ b/main.py @@ -33,21 +33,15 @@ def __init__(self): self.speech = SpeechProcessor() def process_nlp(self, prompt: str) -> str: - """ - Processes text using the NLP module. - """ + """Processes text using the NLP module.""" return self.nlp.generate_text(prompt) def process_cv(self, image: Image.Image) -> str: - """ - Processes an image using the CV module. - """ + """Processes an image to detect objects using the CV module.""" return self.cv.detect_objects(image) def process_stt(self, file: UploadFile) -> str: - """ - Processes audio using the STT module. - """ + """Processes audio and converts it to text using the STT module.""" return self.speech.speech_to_text(file) def process_tts(self, text: str) -> None: @@ -62,18 +56,14 @@ def process_tts(self, text: str) -> None: def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)): - """ - Verifies the Bearer token in the Authorization header. - """ + """Verifies the Bearer token in the Authorization header.""" if credentials.credentials != VALID_API_KEY: raise HTTPException(status_code=403, detail="Forbidden") @app.post("/process-nlp/", dependencies=[Depends(verify_token)]) async def process_nlp(data: dict): - """ - Endpoint for text generation. - """ + """Processes natural language input and generates a response.""" try: prompt = data.get("text", "") return {"response": agi.process_nlp(prompt)} @@ -84,9 +74,7 @@ async def process_nlp(data: dict): @app.post("/process-cv-detection/", dependencies=[Depends(verify_token)]) async def process_cv_detection(file: UploadFile = File(...)): - """ - Endpoint for object detection in images. - """ + """Handles object detection in uploaded images.""" try: image_data = await file.read() image = Image.open(BytesIO(image_data)) @@ -98,9 +86,7 @@ async def process_cv_detection(file: UploadFile = File(...)): @app.post("/speech-to-text/", dependencies=[Depends(verify_token)]) async def speech_to_text(file: UploadFile = File(...)): - """ - Endpoint for Speech-to-Text conversion. - """ + """Converts speech from an uploaded file to text.""" try: return {"response": agi.process_stt(file)} except Exception as e: @@ -110,9 +96,7 @@ async def speech_to_text(file: UploadFile = File(...)): @app.post("/text-to-speech/", dependencies=[Depends(verify_token)]) async def text_to_speech(data: dict): - """ - Endpoint for Text-to-Speech conversion. - """ + """Converts text to speech.""" try: text = data.get("text", "") agi.process_tts(text) diff --git a/nlp_module.py b/nlp_module.py index 95a7efa..8b70e38 100644 --- a/nlp_module.py +++ b/nlp_module.py @@ -27,15 +27,11 @@ def __init__(self): logger.info("NLP model loaded successfully.") def generate_text(self, prompt: str) -> str: - """ - Generates text based on the provided prompt. - + """Generates text based on the provided prompt. + Args: prompt (str): The input text to process. - - Returns: - str: The generated response. - + Raises: ValueError: If the prompt is empty. """