5
5
"colab" : {
6
6
"private_outputs" : true ,
7
7
"provenance" : [],
8
- "authorship_tag" : " ABX9TyPedi3hlUYgSbkDSkl23mSH " ,
8
+ "authorship_tag" : " ABX9TyPoHH519BuqGSnR/HON75UP " ,
9
9
"include_colab_link" : true
10
10
},
11
11
"kernelspec" : {
29
29
},
30
30
{
31
31
"cell_type" : " code" ,
32
- "execution_count" : null ,
33
- "metadata" : {
34
- "id" : " TgxJtnG273_l"
35
- },
36
- "outputs" : [],
37
32
"source" : [
38
33
" # === Imports ===\n " ,
39
34
" import os\n " ,
40
35
" import asyncio\n " ,
36
+ " import time\n " ,
37
+ " from typing import List\n " ,
41
38
" import torch\n " ,
42
39
" from transformers import T5Tokenizer, T5ForConditionalGeneration\n " ,
43
40
" from PIL import Image\n " ,
44
- " from fastapi import FastAPI, UploadFile, Depends, HTTPException\n " ,
41
+ " from fastapi import FastAPI, UploadFile, Depends, HTTPException, Request \n " ,
45
42
" from fastapi.security import OAuth2PasswordBearer\n " ,
46
- " from pydantic import BaseModel\n " ,
43
+ " from pydantic import BaseModel, SecretStr \n " ,
47
44
" import whisper\n " ,
48
45
" from ultralytics import YOLO\n " ,
49
46
" import pyttsx3\n " ,
54
51
" \n " ,
55
52
" # === Logging Setup ===\n " ,
56
53
" logger.add(\" pipeline_{time}.log\" , rotation=\" 1 MB\" , level=\" DEBUG\" , enqueue=True, backtrace=True, diagnose=True)\n " ,
54
+ " logger.info(\" Application startup\" )\n " ,
57
55
" \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 " ,
60
60
" oauth2_scheme = OAuth2PasswordBearer(tokenUrl=\" token\" )\n " ,
61
61
" \n " ,
62
+ " # === Authentication Function ===\n " ,
62
63
" def authenticate_user(token: str = Depends(oauth2_scheme)):\n " ,
63
- " if token != SECURE_TOKEN:\n " ,
64
+ " if token != SECURE_TOKEN.get_secret_value() :\n " ,
64
65
" logger.warning(\" Authentication failed.\" )\n " ,
65
66
" raise HTTPException(status_code=401, detail=\" Invalid token\" )\n " ,
66
67
" \n " ,
67
- " # === Request and Response Models ===\n " ,
68
+ " # === Request and Response Models (Pydantic) ===\n " ,
68
69
" class TextRequest(BaseModel):\n " ,
69
70
" text: str\n " ,
70
71
" \n " ,
71
72
" class TextResponse(BaseModel):\n " ,
72
73
" response: str\n " ,
73
74
" \n " ,
74
- " # === NLP Module ===\n " ,
75
+ " # === NLP Module (T5 Transformer) ===\n " ,
75
76
" class NLPModule:\n " ,
76
77
" def __init__(self):\n " ,
77
78
" 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 " ,
85
82
" \n " ,
86
83
" def generate_text(self, prompt: str) -> str:\n " ,
87
84
" if not prompt.strip():\n " ,
88
85
" raise ValueError(\" Prompt cannot be empty.\" )\n " ,
89
86
" 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 " ,
101
94
" class CVModule:\n " ,
102
95
" 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 " ,
110
99
" \n " ,
111
100
" def detect_objects(self, image: Image.Image) -> str:\n " ,
112
101
" 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 " ,
121
106
" class SpeechProcessor:\n " ,
122
107
" 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 " ,
131
111
" \n " ,
132
112
" 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 " ,
137
115
" 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 " ,
141
116
" \n " ,
142
117
" def text_to_speech(self, text: str) -> None:\n " ,
143
118
" if not text.strip():\n " ,
144
119
" 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 " ,
152
122
" \n " ,
153
123
" def __del__(self):\n " ,
154
124
" self.tts.stop()\n " ,
161
131
" self.speech_processor = SpeechProcessor()\n " ,
162
132
" \n " ,
163
133
" 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 " ,
165
135
" \n " ,
166
136
" async def process_cv(self, image: Image.Image) -> str:\n " ,
167
137
" return await asyncio.to_thread(self.cv.detect_objects, image)\n " ,
174
144
" \n " ,
175
145
" # === FastAPI Application ===\n " ,
176
146
" app = FastAPI()\n " ,
147
+ " \n " ,
177
148
" pipeline = EnhancedAGIPipeline()\n " ,
178
149
" \n " ,
150
+ " # === Endpoints ===\n " ,
179
151
" @app.post(\" /process-nlp/\" , response_model=TextResponse, dependencies=[Depends(authenticate_user)])\n " ,
180
152
" 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 " ,
188
155
" \n " ,
189
156
" @app.post(\" /process-cv-detection/\" , dependencies=[Depends(authenticate_user)])\n " ,
190
157
" 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 " ,
192
166
" image = Image.open(io.BytesIO(await file.read()))\n " ,
193
167
" 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 " ,
199
170
" \n " ,
200
171
" @app.post(\" /speech-to-text/\" , response_model=TextResponse, dependencies=[Depends(authenticate_user)])\n " ,
201
172
" 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 " ,
209
175
" \n " ,
210
176
" @app.post(\" /text-to-speech/\" , dependencies=[Depends(authenticate_user)])\n " ,
211
177
" 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 " ,
221
182
" if __name__ == \" __main__\" :\n " ,
222
183
" nest_asyncio.apply()\n " ,
223
184
" config = uvicorn.Config(app, host=\" 0.0.0.0\" , port=8000)\n " ,
224
185
" server = uvicorn.Server(config)\n " ,
225
186
" asyncio.run(server.serve())"
226
- ]
187
+ ],
188
+ "metadata" : {
189
+ "id" : " UgUAMujBWqGS"
190
+ },
191
+ "execution_count" : null ,
192
+ "outputs" : []
227
193
}
228
194
]
229
195
}
0 commit comments