Spaces:
Paused
Paused
Update app/main.py
Browse files- app/main.py +158 -12
app/main.py
CHANGED
|
@@ -1,13 +1,25 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from app.speech_to_text import transcribe_audio
|
| 5 |
from app.text_to_speech import synthesize_speech
|
| 6 |
-
from fastapi.responses import StreamingResponse, JSONResponse
|
| 7 |
-
import io
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
allow_origins=["*"],
|
|
@@ -15,20 +27,154 @@ app.add_middleware(
|
|
| 15 |
allow_headers=["*"],
|
| 16 |
)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@app.post("/transcribe")
|
| 19 |
async def transcribe(file: UploadFile = File(...)):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
@app.post("/query")
|
| 25 |
async def query_agent(request: Request):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
@app.get("/speak")
|
| 32 |
async def speak(text: str):
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
from fastapi.responses import StreamingResponse, JSONResponse, FileResponse
|
| 5 |
+
import io
|
| 6 |
+
import os
|
| 7 |
+
import logging
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
# Import our modules
|
| 11 |
+
from app.agent import process_text, clear_memory
|
| 12 |
from app.speech_to_text import transcribe_audio
|
| 13 |
from app.text_to_speech import synthesize_speech
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Configure logging
|
| 16 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
|
| 19 |
+
# Create the FastAPI app
|
| 20 |
app = FastAPI()
|
| 21 |
|
| 22 |
+
# Add CORS middleware
|
| 23 |
app.add_middleware(
|
| 24 |
CORSMiddleware,
|
| 25 |
allow_origins=["*"],
|
|
|
|
| 27 |
allow_headers=["*"],
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# Create static directory if it doesn't exist
|
| 31 |
+
static_dir = Path("static")
|
| 32 |
+
static_dir.mkdir(exist_ok=True)
|
| 33 |
+
|
| 34 |
+
# Mount static files if directory exists
|
| 35 |
+
if static_dir.exists():
|
| 36 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 37 |
+
|
| 38 |
+
# Routes for the API
|
| 39 |
+
@app.get("/")
|
| 40 |
+
async def root():
|
| 41 |
+
"""Serve the main UI if available."""
|
| 42 |
+
index_path = static_dir / "index.html"
|
| 43 |
+
if index_path.exists():
|
| 44 |
+
return FileResponse(index_path)
|
| 45 |
+
return {"message": "AGI Telecom POC API - UI not available"}
|
| 46 |
+
|
| 47 |
+
@app.get("/health")
|
| 48 |
+
async def health_check():
|
| 49 |
+
"""Health check endpoint."""
|
| 50 |
+
return {"status": "ok"}
|
| 51 |
+
|
| 52 |
@app.post("/transcribe")
|
| 53 |
async def transcribe(file: UploadFile = File(...)):
|
| 54 |
+
"""
|
| 55 |
+
Transcribe audio to text.
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
file: Audio file upload
|
| 59 |
+
|
| 60 |
+
Returns:
|
| 61 |
+
JSON with transcription
|
| 62 |
+
"""
|
| 63 |
+
try:
|
| 64 |
+
audio_bytes = await file.read()
|
| 65 |
+
text = transcribe_audio(audio_bytes)
|
| 66 |
+
logger.info(f"Transcribed: {text[:30]}...")
|
| 67 |
+
return {"transcription": text}
|
| 68 |
+
except Exception as e:
|
| 69 |
+
logger.error(f"Transcription error: {str(e)}")
|
| 70 |
+
return JSONResponse(
|
| 71 |
+
status_code=500,
|
| 72 |
+
content={"error": f"Failed to transcribe audio: {str(e)}"}
|
| 73 |
+
)
|
| 74 |
|
| 75 |
@app.post("/query")
|
| 76 |
async def query_agent(request: Request):
|
| 77 |
+
"""
|
| 78 |
+
Process a text query with the agent.
|
| 79 |
+
|
| 80 |
+
Args:
|
| 81 |
+
request: Request with input_text in JSON body
|
| 82 |
+
|
| 83 |
+
Returns:
|
| 84 |
+
JSON with agent response
|
| 85 |
+
"""
|
| 86 |
+
try:
|
| 87 |
+
data = await request.json()
|
| 88 |
+
input_text = data.get("input_text", "")
|
| 89 |
+
if not input_text:
|
| 90 |
+
return JSONResponse(
|
| 91 |
+
status_code=400,
|
| 92 |
+
content={"error": "No input_text provided"}
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
response = process_text(input_text)
|
| 96 |
+
logger.info(f"Query: {input_text[:30]}... Response: {response[:30]}...")
|
| 97 |
+
return {"response": response}
|
| 98 |
+
except Exception as e:
|
| 99 |
+
logger.error(f"Query error: {str(e)}")
|
| 100 |
+
return JSONResponse(
|
| 101 |
+
status_code=500,
|
| 102 |
+
content={"error": f"Failed to process query: {str(e)}"}
|
| 103 |
+
)
|
| 104 |
|
| 105 |
@app.get("/speak")
|
| 106 |
async def speak(text: str):
|
| 107 |
+
"""
|
| 108 |
+
Convert text to speech.
|
| 109 |
+
|
| 110 |
+
Args:
|
| 111 |
+
text: Text to convert to speech
|
| 112 |
+
|
| 113 |
+
Returns:
|
| 114 |
+
Audio stream
|
| 115 |
+
"""
|
| 116 |
+
try:
|
| 117 |
+
audio = synthesize_speech(text)
|
| 118 |
+
return StreamingResponse(io.BytesIO(audio), media_type="audio/mpeg")
|
| 119 |
+
except Exception as e:
|
| 120 |
+
logger.error(f"Speech synthesis error: {str(e)}")
|
| 121 |
+
return JSONResponse(
|
| 122 |
+
status_code=500,
|
| 123 |
+
content={"error": f"Failed to synthesize speech: {str(e)}"}
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
@app.post("/clear_memory")
|
| 127 |
+
async def reset_memory():
|
| 128 |
+
"""Clear the conversation memory."""
|
| 129 |
+
success = clear_memory()
|
| 130 |
+
return {"success": success}
|
| 131 |
+
|
| 132 |
+
@app.post("/complete_flow")
|
| 133 |
+
async def complete_flow(request: Request):
|
| 134 |
+
"""
|
| 135 |
+
Complete flow: audio to text to agent to speech.
|
| 136 |
+
|
| 137 |
+
Args:
|
| 138 |
+
request: Request with audio_base64 or text_input in JSON body
|
| 139 |
+
|
| 140 |
+
Returns:
|
| 141 |
+
JSON with results and audio URL
|
| 142 |
+
"""
|
| 143 |
+
try:
|
| 144 |
+
data = await request.json()
|
| 145 |
+
text_input = data.get("text_input")
|
| 146 |
+
|
| 147 |
+
# Process with agent
|
| 148 |
+
if not text_input:
|
| 149 |
+
return JSONResponse(
|
| 150 |
+
status_code=400,
|
| 151 |
+
content={"error": "No input provided"}
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
response = process_text(text_input)
|
| 155 |
+
logger.info(f"Agent response: {response[:30]}...")
|
| 156 |
+
|
| 157 |
+
# Return the response
|
| 158 |
+
return {
|
| 159 |
+
"input": text_input,
|
| 160 |
+
"response": response
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
except Exception as e:
|
| 164 |
+
logger.error(f"Complete flow error: {str(e)}")
|
| 165 |
+
return JSONResponse(
|
| 166 |
+
status_code=500,
|
| 167 |
+
content={"error": f"Failed to process: {str(e)}"}
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
# Run the app
|
| 171 |
+
if __name__ == "__main__":
|
| 172 |
+
import uvicorn
|
| 173 |
+
# Check if running on HF Spaces
|
| 174 |
+
if os.environ.get("SPACE_ID"):
|
| 175 |
+
# Running on HF Spaces - use their port
|
| 176 |
+
port = int(os.environ.get("PORT", 7860))
|
| 177 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
| 178 |
+
else:
|
| 179 |
+
# Running locally
|
| 180 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|