Skip to content

Commit b2ec456

Browse files
Created using Colab
1 parent cab54f4 commit b2ec456

File tree

1 file changed

+65
-99
lines changed

1 file changed

+65
-99
lines changed

agi_pipeline.ipynb

Lines changed: 65 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"colab": {
66
"private_outputs": true,
77
"provenance": [],
8-
"authorship_tag": "ABX9TyPedi3hlUYgSbkDSkl23mSH",
8+
"authorship_tag": "ABX9TyPoHH519BuqGSnR/HON75UP",
99
"include_colab_link": true
1010
},
1111
"kernelspec": {
@@ -29,21 +29,18 @@
2929
},
3030
{
3131
"cell_type": "code",
32-
"execution_count": null,
33-
"metadata": {
34-
"id": "TgxJtnG273_l"
35-
},
36-
"outputs": [],
3732
"source": [
3833
"# === Imports ===\n",
3934
"import os\n",
4035
"import asyncio\n",
36+
"import time\n",
37+
"from typing import List\n",
4138
"import torch\n",
4239
"from transformers import T5Tokenizer, T5ForConditionalGeneration\n",
4340
"from PIL import Image\n",
44-
"from fastapi import FastAPI, UploadFile, Depends, HTTPException\n",
41+
"from fastapi import FastAPI, UploadFile, Depends, HTTPException, Request\n",
4542
"from fastapi.security import OAuth2PasswordBearer\n",
46-
"from pydantic import BaseModel\n",
43+
"from pydantic import BaseModel, SecretStr\n",
4744
"import whisper\n",
4845
"from ultralytics import YOLO\n",
4946
"import pyttsx3\n",
@@ -54,101 +51,74 @@
5451
"\n",
5552
"# === Logging Setup ===\n",
5653
"logger.add(\"pipeline_{time}.log\", rotation=\"1 MB\", level=\"DEBUG\", enqueue=True, backtrace=True, diagnose=True)\n",
54+
"logger.info(\"Application startup\")\n",
5755
"\n",
58-
"# === Environment Variables and Authentication ===\n",
59-
"SECURE_TOKEN = os.getenv(\"SECURE_TOKEN\", \"your_actual_secure_token\")\n",
56+
"# === Security Enhancement: Environment Variable for Secure Token ===\n",
57+
"SECURE_TOKEN = SecretStr(os.getenv(\"SECURE_TOKEN\", \"YvZz9Hni0hWJPh_UWW4dQYf9rhIe9nNYcC5ZQTTZz0Q\"))\n",
58+
"\n",
59+
"# === OAuth2PasswordBearer for Authentication ===\n",
6060
"oauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n",
6161
"\n",
62+
"# === Authentication Function ===\n",
6263
"def authenticate_user(token: str = Depends(oauth2_scheme)):\n",
63-
" if token != SECURE_TOKEN:\n",
64+
" if token != SECURE_TOKEN.get_secret_value():\n",
6465
" logger.warning(\"Authentication failed.\")\n",
6566
" raise HTTPException(status_code=401, detail=\"Invalid token\")\n",
6667
"\n",
67-
"# === Request and Response Models ===\n",
68+
"# === Request and Response Models (Pydantic) ===\n",
6869
"class TextRequest(BaseModel):\n",
6970
" text: str\n",
7071
"\n",
7172
"class TextResponse(BaseModel):\n",
7273
" response: str\n",
7374
"\n",
74-
"# === NLP Module ===\n",
75+
"# === NLP Module (T5 Transformer) ===\n",
7576
"class NLPModule:\n",
7677
" def __init__(self):\n",
7778
" model_name = \"google/flan-t5-small\"\n",
78-
" try:\n",
79-
" self.tokenizer = T5Tokenizer.from_pretrained(model_name)\n",
80-
" self.model = T5ForConditionalGeneration.from_pretrained(model_name)\n",
81-
" logger.info(\"NLP model loaded successfully.\")\n",
82-
" except Exception as e:\n",
83-
" logger.error(f\"Failed to load NLP model: {e}\")\n",
84-
" raise RuntimeError(\"Failed to load NLP model.\")\n",
79+
" self.tokenizer = T5Tokenizer.from_pretrained(model_name)\n",
80+
" self.model = T5ForConditionalGeneration.from_pretrained(model_name)\n",
81+
" logger.info(\"NLP model loaded successfully.\")\n",
8582
"\n",
8683
" def generate_text(self, prompt: str) -> str:\n",
8784
" if not prompt.strip():\n",
8885
" raise ValueError(\"Prompt cannot be empty.\")\n",
8986
" logger.debug(f\"Generating text for prompt: {prompt}\")\n",
90-
" try:\n",
91-
" inputs = self.tokenizer(prompt, return_tensors=\"pt\")\n",
92-
" outputs = self.model.generate(inputs[\"input_ids\"], max_length=100)\n",
93-
" response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
94-
" logger.info(f\"Generated response: {response}\")\n",
95-
" return response\n",
96-
" except Exception as e:\n",
97-
" logger.error(f\"Error in text generation: {e}\")\n",
98-
" raise RuntimeError(\"Text generation failed.\")\n",
99-
"\n",
100-
"# === CV Module with Object Detection ===\n",
87+
" inputs = self.tokenizer(prompt, return_tensors=\"pt\")\n",
88+
" outputs = self.model.generate(inputs[\"input_ids\"], max_length=100)\n",
89+
" response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
90+
" logger.info(f\"Generated response: {response}\")\n",
91+
" return response\n",
92+
"\n",
93+
"# === CV Module (YOLOv8 for Object Detection) ===\n",
10194
"class CVModule:\n",
10295
" def __init__(self):\n",
103-
" try:\n",
104-
" self.device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
105-
" self.model = YOLO('yolov5su.pt').to(self.device)\n",
106-
" logger.info(\"CV model loaded successfully.\")\n",
107-
" except Exception as e:\n",
108-
" logger.error(f\"Failed to load CV model: {e}\")\n",
109-
" raise RuntimeError(\"Failed to load CV model.\")\n",
96+
" self.device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
97+
" self.model = YOLO('yolov8n.pt').to(self.device)\n",
98+
" logger.info(\"CV model loaded successfully.\")\n",
11099
"\n",
111100
" def detect_objects(self, image: Image.Image) -> str:\n",
112101
" logger.debug(\"Detecting objects in the image.\")\n",
113-
" try:\n",
114-
" results = self.model(image)\n",
115-
" return results.pandas().xyxy[0].to_json()\n",
116-
" except Exception as e:\n",
117-
" logger.error(f\"Object detection failed: {e}\")\n",
118-
" raise ValueError(\"Object detection error.\")\n",
119-
"\n",
120-
"# === Speech Processor ===\n",
102+
" results = self.model(image)\n",
103+
" return results.pandas().xyxy[0].to_json()\n",
104+
"\n",
105+
"# === Speech Processor (Whisper for Speech-to-Text, PyTTSX3 for Text-to-Speech) ===\n",
121106
"class SpeechProcessor:\n",
122107
" def __init__(self):\n",
123-
" try:\n",
124-
" import whisper # Import inside the class to ensure correct package\n",
125-
" self.whisper_model = whisper.load_model(\"base\")\n",
126-
" self.tts = pyttsx3.init()\n",
127-
" logger.info(\"Speech processor initialized successfully.\")\n",
128-
" except Exception as e:\n",
129-
" logger.error(f\"Failed to initialize speech processor: {e}\")\n",
130-
" raise RuntimeError(\"Failed to initialize speech processor.\")\n",
108+
" self.whisper_model = whisper.load_model(\"base\")\n",
109+
" self.tts = pyttsx3.init()\n",
110+
" logger.info(\"Speech processor initialized successfully.\")\n",
131111
"\n",
132112
" def speech_to_text(self, audio_file: UploadFile) -> str:\n",
133-
" logger.debug(\"Processing speech-to-text.\")\n",
134-
" try:\n",
135-
" with audio_file.file as audio_data:\n",
136-
" result = self.whisper_model.transcribe(audio_data)\n",
113+
" with audio_file.file as audio_data:\n",
114+
" result = self.whisper_model.transcribe(audio_data)\n",
137115
" return result['text']\n",
138-
" except Exception as e:\n",
139-
" logger.error(f\"Speech-to-text failed: {e}\")\n",
140-
" raise ValueError(\"Speech-to-text error.\")\n",
141116
"\n",
142117
" def text_to_speech(self, text: str) -> None:\n",
143118
" if not text.strip():\n",
144119
" raise ValueError(\"Text cannot be empty.\")\n",
145-
" logger.debug(\"Processing text-to-speech.\")\n",
146-
" try:\n",
147-
" self.tts.say(text)\n",
148-
" self.tts.runAndWait()\n",
149-
" except Exception as e:\n",
150-
" logger.error(f\"Text-to-speech failed: {e}\")\n",
151-
" raise RuntimeError(\"Text-to-speech error.\")\n",
120+
" self.tts.say(text)\n",
121+
" self.tts.runAndWait()\n",
152122
"\n",
153123
" def __del__(self):\n",
154124
" self.tts.stop()\n",
@@ -161,7 +131,7 @@
161131
" self.speech_processor = SpeechProcessor()\n",
162132
"\n",
163133
" async def process_nlp(self, text: str) -> str:\n",
164-
" return self.nlp.generate_text(text)\n",
134+
" return await asyncio.to_thread(self.nlp.generate_text, text)\n",
165135
"\n",
166136
" async def process_cv(self, image: Image.Image) -> str:\n",
167137
" return await asyncio.to_thread(self.cv.detect_objects, image)\n",
@@ -174,56 +144,52 @@
174144
"\n",
175145
"# === FastAPI Application ===\n",
176146
"app = FastAPI()\n",
147+
"\n",
177148
"pipeline = EnhancedAGIPipeline()\n",
178149
"\n",
150+
"# === Endpoints ===\n",
179151
"@app.post(\"/process-nlp/\", response_model=TextResponse, dependencies=[Depends(authenticate_user)])\n",
180152
"async def process_nlp(request: TextRequest):\n",
181-
" try:\n",
182-
" response = await pipeline.process_nlp(request.text)\n",
183-
" logger.info(\"NLP processed successfully.\")\n",
184-
" return {\"response\": response}\n",
185-
" except Exception as e:\n",
186-
" logger.error(f\"NLP processing failed: {e}\")\n",
187-
" raise HTTPException(status_code=500, detail=\"NLP processing error.\")\n",
153+
" response = await pipeline.process_nlp(request.text)\n",
154+
" return {\"response\": response}\n",
188155
"\n",
189156
"@app.post(\"/process-cv-detection/\", dependencies=[Depends(authenticate_user)])\n",
190157
"async def process_cv_detection(file: UploadFile):\n",
191-
" try:\n",
158+
" image = Image.open(io.BytesIO(await file.read()))\n",
159+
" response = await pipeline.process_cv(image)\n",
160+
" return {\"detections\": response}\n",
161+
"\n",
162+
"@app.post(\"/batch-cv-detection/\", dependencies=[Depends(authenticate_user)])\n",
163+
"async def batch_cv_detection(files: List[UploadFile]):\n",
164+
" responses = []\n",
165+
" for file in files:\n",
192166
" image = Image.open(io.BytesIO(await file.read()))\n",
193167
" response = await pipeline.process_cv(image)\n",
194-
" logger.info(\"Object detection processed successfully.\")\n",
195-
" return {\"detections\": response}\n",
196-
" except Exception as e:\n",
197-
" logger.error(f\"Object detection failed: {e}\")\n",
198-
" raise HTTPException(status_code=500, detail=\"Object detection error.\")\n",
168+
" responses.append(response)\n",
169+
" return {\"batch_detections\": responses}\n",
199170
"\n",
200171
"@app.post(\"/speech-to-text/\", response_model=TextResponse, dependencies=[Depends(authenticate_user)])\n",
201172
"async def speech_to_text(file: UploadFile):\n",
202-
" try:\n",
203-
" response = await pipeline.process_speech_to_text(file)\n",
204-
" logger.info(\"Speech-to-text processed successfully.\")\n",
205-
" return {\"response\": response}\n",
206-
" except Exception as e:\n",
207-
" logger.error(f\"Speech-to-text failed: {e}\")\n",
208-
" raise HTTPException(status_code=500, detail=\"Speech-to-text error.\")\n",
173+
" response = await pipeline.process_speech_to_text(file)\n",
174+
" return {\"response\": response}\n",
209175
"\n",
210176
"@app.post(\"/text-to-speech/\", dependencies=[Depends(authenticate_user)])\n",
211177
"async def text_to_speech(request: TextRequest):\n",
212-
" try:\n",
213-
" await pipeline.process_text_to_speech(request.text)\n",
214-
" logger.info(\"Text-to-speech processed successfully.\")\n",
215-
" return {\"response\": \"Speech synthesis complete.\"}\n",
216-
" except Exception as e:\n",
217-
" logger.error(f\"Text-to-speech failed: {e}\")\n",
218-
" raise HTTPException(status_code=500, detail=\"Text-to-speech error.\")\n",
219-
"\n",
220-
"# === Run the Application with HTTPS ===\n",
178+
" await pipeline.process_text_to_speech(request.text)\n",
179+
" return {\"response\": \"Speech synthesis complete.\"}\n",
180+
"\n",
181+
"# === Run the Application with HTTPS (uvicorn) ===\n",
221182
"if __name__ == \"__main__\":\n",
222183
" nest_asyncio.apply()\n",
223184
" config = uvicorn.Config(app, host=\"0.0.0.0\", port=8000)\n",
224185
" server = uvicorn.Server(config)\n",
225186
" asyncio.run(server.serve())"
226-
]
187+
],
188+
"metadata": {
189+
"id": "UgUAMujBWqGS"
190+
},
191+
"execution_count": null,
192+
"outputs": []
227193
}
228194
]
229195
}

0 commit comments

Comments
 (0)