Skip to content

Commit f5a0e9c

Browse files
Created using Colab
1 parent 4d75ec9 commit f5a0e9c

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

agi_pipeline.ipynb

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"colab": {
66
"private_outputs": true,
77
"provenance": [],
8-
"authorship_tag": "ABX9TyPoHH519BuqGSnR/HON75UP",
8+
"authorship_tag": "ABX9TyNk2WJFhV2VGr1cGsUqJ/ai",
99
"include_colab_link": true
1010
},
1111
"kernelspec": {
@@ -30,42 +30,38 @@
3030
{
3131
"cell_type": "code",
3232
"source": [
33-
"# === Imports ===\n",
3433
"import os\n",
3534
"import asyncio\n",
36-
"import time\n",
37-
"from typing import List\n",
3835
"import torch\n",
39-
"from transformers import T5Tokenizer, T5ForConditionalGeneration\n",
36+
"from typing import List\n",
4037
"from PIL import Image\n",
41-
"from fastapi import FastAPI, UploadFile, Depends, HTTPException, Request\n",
38+
"from fastapi import FastAPI, UploadFile, Depends, HTTPException\n",
4239
"from fastapi.security import OAuth2PasswordBearer\n",
4340
"from pydantic import BaseModel, SecretStr\n",
44-
"import whisper\n",
45-
"from ultralytics import YOLO\n",
4641
"import pyttsx3\n",
4742
"from loguru import logger\n",
4843
"import io\n",
49-
"import nest_asyncio\n",
5044
"import uvicorn\n",
45+
"import nest_asyncio\n",
46+
"from transformers import T5Tokenizer, T5ForConditionalGeneration\n",
47+
"from ultralytics import YOLO\n",
48+
"import whisper\n",
5149
"\n",
52-
"# === Logging Setup ===\n",
50+
"# === Configuration and Logging Setup ===\n",
51+
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
5352
"logger.add(\"pipeline_{time}.log\", rotation=\"1 MB\", level=\"DEBUG\", enqueue=True, backtrace=True, diagnose=True)\n",
5453
"logger.info(\"Application startup\")\n",
5554
"\n",
56-
"# === Security Enhancement: Environment Variable for Secure Token ===\n",
55+
"# === Security Setup ===\n",
5756
"SECURE_TOKEN = SecretStr(os.getenv(\"SECURE_TOKEN\", \"YvZz9Hni0hWJPh_UWW4dQYf9rhIe9nNYcC5ZQTTZz0Q\"))\n",
58-
"\n",
59-
"# === OAuth2PasswordBearer for Authentication ===\n",
6057
"oauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n",
6158
"\n",
62-
"# === Authentication Function ===\n",
6359
"def authenticate_user(token: str = Depends(oauth2_scheme)):\n",
6460
" if token != SECURE_TOKEN.get_secret_value():\n",
6561
" logger.warning(\"Authentication failed.\")\n",
6662
" raise HTTPException(status_code=401, detail=\"Invalid token\")\n",
6763
"\n",
68-
"# === Request and Response Models (Pydantic) ===\n",
64+
"# === Pydantic Models ===\n",
6965
"class TextRequest(BaseModel):\n",
7066
" text: str\n",
7167
"\n",
@@ -84,17 +80,17 @@
8480
" if not prompt.strip():\n",
8581
" raise ValueError(\"Prompt cannot be empty.\")\n",
8682
" logger.debug(f\"Generating text for prompt: {prompt}\")\n",
87-
" inputs = self.tokenizer(prompt, return_tensors=\"pt\")\n",
88-
" outputs = self.model.generate(inputs[\"input_ids\"], max_length=100)\n",
83+
" inputs = self.tokenizer(prompt, return_tensors=\"pt\").to(device)\n",
84+
" with torch.no_grad():\n",
85+
" outputs = self.model.generate(inputs[\"input_ids\"], max_length=100)\n",
8986
" response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
9087
" logger.info(f\"Generated response: {response}\")\n",
9188
" return response\n",
9289
"\n",
9390
"# === CV Module (YOLOv8 for Object Detection) ===\n",
9491
"class CVModule:\n",
9592
" def __init__(self):\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",
93+
" self.model = YOLO('yolov8n.pt').to(device)\n",
9894
" logger.info(\"CV model loaded successfully.\")\n",
9995
"\n",
10096
" def detect_objects(self, image: Image.Image) -> str:\n",
@@ -181,9 +177,7 @@
181177
"# === Run the Application with HTTPS (uvicorn) ===\n",
182178
"if __name__ == \"__main__\":\n",
183179
" nest_asyncio.apply()\n",
184-
" config = uvicorn.Config(app, host=\"0.0.0.0\", port=8000)\n",
185-
" server = uvicorn.Server(config)\n",
186-
" asyncio.run(server.serve())"
180+
" uvicorn.run(app, host=\"0.0.0.0\", port=8000)"
187181
],
188182
"metadata": {
189183
"id": "UgUAMujBWqGS"

0 commit comments

Comments
 (0)