-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
112 lines (86 loc) · 3.49 KB
/
main.py
File metadata and controls
112 lines (86 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Main entry point for the Enhanced AGI Pipeline API.
"""
import os
from io import BytesIO
from fastapi import FastAPI, UploadFile, File, HTTPException, Depends
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from PIL import Image
from loguru import logger
from nlp_module import NLPModule
from cv_module import CVModule
from speech_processor import SpeechProcessor
# API Key from environment or default
VALID_API_KEY = os.getenv("AGI_API_KEY", "YvZz9Hni0hWJPh_UWW4dQYf9rhIe9nNYcC5ZQTTZz0Q")
security = HTTPBearer()
class EnhancedAGIPipeline:
"""
A wrapper class that integrates NLP, CV, and Speech modules.
"""
def __init__(self):
"""
Initializes all pipeline modules.
"""
self.nlp = NLPModule()
self.cv = CVModule()
self.speech = SpeechProcessor()
def process_nlp(self, prompt: str) -> str:
"""Processes text using the NLP module."""
return self.nlp.generate_text(prompt)
def process_cv(self, image: Image.Image) -> str:
"""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 and converts it to text using the STT module."""
return self.speech.speech_to_text(file)
def process_tts(self, text: str) -> None:
"""
Processes text using the TTS module.
"""
self.speech.text_to_speech(text)
app = FastAPI()
agi = EnhancedAGIPipeline()
def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
"""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):
"""Processes natural language input and generates a response."""
try:
prompt = data.get("text", "")
return {"response": agi.process_nlp(prompt)}
except Exception as e:
logger.error(f"NLP Error: {e}")
raise HTTPException(status_code=500, detail=str(e)) from e
@app.post("/process-cv-detection/", dependencies=[Depends(verify_token)])
async def process_cv_detection(file: UploadFile = File(...)):
"""Handles object detection in uploaded images."""
try:
image_data = await file.read()
image = Image.open(BytesIO(image_data))
return {"detections": agi.process_cv(image)}
except Exception as e:
logger.error(f"CV Error: {e}")
raise HTTPException(status_code=500, detail=str(e)) from e
@app.post("/speech-to-text/", dependencies=[Depends(verify_token)])
async def speech_to_text(file: UploadFile = File(...)):
"""Converts speech from an uploaded file to text."""
try:
return {"response": agi.process_stt(file)}
except Exception as e:
logger.error(f"STT Error: {e}")
raise HTTPException(status_code=500, detail=str(e)) from e
@app.post("/text-to-speech/", dependencies=[Depends(verify_token)])
async def text_to_speech(data: dict):
"""Converts text to speech."""
try:
text = data.get("text", "")
agi.process_tts(text)
return {"response": "Speech synthesis complete."}
except Exception as e:
logger.error(f"TTS Error: {e}")
raise HTTPException(status_code=500, detail=str(e)) from e
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)