Egyptian_Chatbot / app /main.py
SherinMohamed's picture
Update app/main.py
90317cf verified
Raw History Blame Contribute Delete
1.49 kB
import os
import shutil
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import FileResponse, JSONResponse
from app.pipeline import FullEgyptianVoiceChatbot
app = FastAPI(title="Egyptian Arabic Voice Chatbot (JAIS + EGTTS)")
bot = FullEgyptianVoiceChatbot()
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(title="Egyptian Arabic Voice Chatbot (JAIS + EGTTS)")
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000", "http://127.0.0.1:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health")
def health():
return {"status": "ok"}
@app.post("/chat/text")
async def chat_text(payload: dict):
text = (payload.get("text") or "").strip()
if not text:
return JSONResponse({"error": "Missing text"}, status_code=400)
result = bot.chat_text(text)
return result
@app.post("/chat/voice")
async def chat_voice(file: UploadFile = File(...)):
os.makedirs("uploads", exist_ok=True)
tmp_path = f"uploads/{file.filename}"
with open(tmp_path, "wb") as f:
shutil.copyfileobj(file.file, f)
result = bot.chat_voice(tmp_path)
return result
@app.get("/audio/{audio_id}")
def get_audio(audio_id: str):
path = f"outputs/{audio_id}.wav"
if not os.path.exists(path):
return JSONResponse({"error": "Audio not found"}, status_code=404)
return FileResponse(path, media_type="audio/wav", filename=f"{audio_id}.wav")