|
5 | 5 | "colab": { |
6 | 6 | "private_outputs": true, |
7 | 7 | "provenance": [], |
8 | | - "authorship_tag": "ABX9TyPoHH519BuqGSnR/HON75UP", |
| 8 | + "authorship_tag": "ABX9TyNk2WJFhV2VGr1cGsUqJ/ai", |
9 | 9 | "include_colab_link": true |
10 | 10 | }, |
11 | 11 | "kernelspec": { |
|
30 | 30 | { |
31 | 31 | "cell_type": "code", |
32 | 32 | "source": [ |
33 | | - "# === Imports ===\n", |
34 | 33 | "import os\n", |
35 | 34 | "import asyncio\n", |
36 | | - "import time\n", |
37 | | - "from typing import List\n", |
38 | 35 | "import torch\n", |
39 | | - "from transformers import T5Tokenizer, T5ForConditionalGeneration\n", |
| 36 | + "from typing import List\n", |
40 | 37 | "from PIL import Image\n", |
41 | | - "from fastapi import FastAPI, UploadFile, Depends, HTTPException, Request\n", |
| 38 | + "from fastapi import FastAPI, UploadFile, Depends, HTTPException\n", |
42 | 39 | "from fastapi.security import OAuth2PasswordBearer\n", |
43 | 40 | "from pydantic import BaseModel, SecretStr\n", |
44 | | - "import whisper\n", |
45 | | - "from ultralytics import YOLO\n", |
46 | 41 | "import pyttsx3\n", |
47 | 42 | "from loguru import logger\n", |
48 | 43 | "import io\n", |
49 | | - "import nest_asyncio\n", |
50 | 44 | "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", |
51 | 49 | "\n", |
52 | | - "# === Logging Setup ===\n", |
| 50 | + "# === Configuration and Logging Setup ===\n", |
| 51 | + "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", |
53 | 52 | "logger.add(\"pipeline_{time}.log\", rotation=\"1 MB\", level=\"DEBUG\", enqueue=True, backtrace=True, diagnose=True)\n", |
54 | 53 | "logger.info(\"Application startup\")\n", |
55 | 54 | "\n", |
56 | | - "# === Security Enhancement: Environment Variable for Secure Token ===\n", |
| 55 | + "# === Security Setup ===\n", |
57 | 56 | "SECURE_TOKEN = SecretStr(os.getenv(\"SECURE_TOKEN\", \"YvZz9Hni0hWJPh_UWW4dQYf9rhIe9nNYcC5ZQTTZz0Q\"))\n", |
58 | | - "\n", |
59 | | - "# === OAuth2PasswordBearer for Authentication ===\n", |
60 | 57 | "oauth2_scheme = OAuth2PasswordBearer(tokenUrl=\"token\")\n", |
61 | 58 | "\n", |
62 | | - "# === Authentication Function ===\n", |
63 | 59 | "def authenticate_user(token: str = Depends(oauth2_scheme)):\n", |
64 | 60 | " if token != SECURE_TOKEN.get_secret_value():\n", |
65 | 61 | " logger.warning(\"Authentication failed.\")\n", |
66 | 62 | " raise HTTPException(status_code=401, detail=\"Invalid token\")\n", |
67 | 63 | "\n", |
68 | | - "# === Request and Response Models (Pydantic) ===\n", |
| 64 | + "# === Pydantic Models ===\n", |
69 | 65 | "class TextRequest(BaseModel):\n", |
70 | 66 | " text: str\n", |
71 | 67 | "\n", |
|
84 | 80 | " if not prompt.strip():\n", |
85 | 81 | " raise ValueError(\"Prompt cannot be empty.\")\n", |
86 | 82 | " 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", |
89 | 86 | " response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)\n", |
90 | 87 | " logger.info(f\"Generated response: {response}\")\n", |
91 | 88 | " return response\n", |
92 | 89 | "\n", |
93 | 90 | "# === CV Module (YOLOv8 for Object Detection) ===\n", |
94 | 91 | "class CVModule:\n", |
95 | 92 | " 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", |
98 | 94 | " logger.info(\"CV model loaded successfully.\")\n", |
99 | 95 | "\n", |
100 | 96 | " def detect_objects(self, image: Image.Image) -> str:\n", |
|
181 | 177 | "# === Run the Application with HTTPS (uvicorn) ===\n", |
182 | 178 | "if __name__ == \"__main__\":\n", |
183 | 179 | " 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)" |
187 | 181 | ], |
188 | 182 | "metadata": { |
189 | 183 | "id": "UgUAMujBWqGS" |
|
0 commit comments