Spaces:
Runtime error
Runtime error
app v1
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import io
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
|
| 9 |
+
from agent import function_caller
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
app.add_middleware(
|
| 14 |
+
CORSMiddleware,
|
| 15 |
+
allow_origins=["*"],
|
| 16 |
+
allow_credentials=True,
|
| 17 |
+
allow_methods=["*"],
|
| 18 |
+
allow_headers=["*"],
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class ChatQuery(BaseModel):
|
| 23 |
+
query: str
|
| 24 |
+
grade: str
|
| 25 |
+
subject: str
|
| 26 |
+
chapter: str
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@app.post("/chat")
|
| 30 |
+
async def chat(query: ChatQuery):
|
| 31 |
+
result = await function_caller(query.query, query.grade, query.subject, query.chapter)
|
| 32 |
+
|
| 33 |
+
if isinstance(result, str):
|
| 34 |
+
return {"text": result}
|
| 35 |
+
elif isinstance(result, bytes) or (isinstance(result, str) and result.startswith("data:audio")):
|
| 36 |
+
if isinstance(result, bytes):
|
| 37 |
+
audio_b64 = base64.b64encode(result).decode()
|
| 38 |
+
else:
|
| 39 |
+
audio_b64 = result.split(",")[1] # Remove the "data:audio/wav;base64," prefix
|
| 40 |
+
return {"audio": audio_b64}
|
| 41 |
+
else:
|
| 42 |
+
return {"error": "Unexpected result type"}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
async def gradio_interface(input_text, grade, subject, chapter):
|
| 46 |
+
response = await chat(ChatQuery(query=input_text, grade=grade, subject=subject, chapter=chapter))
|
| 47 |
+
if "text" in response:
|
| 48 |
+
return response["text"], None
|
| 49 |
+
elif "audio" in response:
|
| 50 |
+
audio_data = base64.b64decode(response["audio"])
|
| 51 |
+
return "Audio response generated", (44100, io.BytesIO(audio_data))
|
| 52 |
+
else:
|
| 53 |
+
return "Unexpected response format", None
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
iface = gr.Interface(
|
| 57 |
+
fn=gradio_interface,
|
| 58 |
+
inputs=[
|
| 59 |
+
gr.Textbox(lines=2, placeholder="Enter your question here..."),
|
| 60 |
+
gr.Dropdown(choices=["1", "2", "3", "4", "5", "6", "7", "9", "10", "11", "12"], label="Grade", value="9", interactive=True),
|
| 61 |
+
gr.Dropdown(choices=["Math", "Science", "History"], label="Subject", value="Science", interactive=True),
|
| 62 |
+
gr.Dropdown(choices=["1", "2", "3", "4", "5", "6", "7", "9", "10", "11", "12", "13", "14", "15", "16"], label="Chapter", value="11", interactive=True),
|
| 63 |
+
],
|
| 64 |
+
outputs=[gr.Textbox(label="Response"), gr.Audio(label="Audio Response")],
|
| 65 |
+
title="Agentic RAG Chatbot",
|
| 66 |
+
description="Ask a question and get an answer from the chatbot. The response may be text or audio.",
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
app = gr.mount_gradio_app(app, iface, path="/")
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
import uvicorn
|
| 73 |
+
|
| 74 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|