forked from cmagganas/newco-jarvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
62 lines (51 loc) · 1.73 KB
/
app.py
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
import os
import dotenv
dotenv.load_dotenv()
import base64
from elevenlabs import generate, set_api_key
# Import FastAPI and other necessary libraries
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from rag import rag_transcript
# Define a FastAPI app
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
# Set up CORS middleware options
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
# Define a Pydantic model for the input data
class QueryInput(BaseModel):
query: str
# Initialize the rag_transcript class
rag_service = rag_transcript()
@app.post("/query")
async def query_rag(input_data: QueryInput):
try:
# Use the rag_transcript method to get the response
response = rag_service.rag_transcript(input_data.query)
# Generate the voice using the provided parameters
voice_id="George"
model="eleven_monolingual_v1"
if elevenlabs_key := os.getenv("ELEVENLABS_API_KEY"):
set_api_key(elevenlabs_key)
# Generate the voice using the provided parameters
generated_audio = generate(
text=response,
voice=voice_id,
model=model
)
audio_b64 = base64.b64encode(generated_audio).decode("utf-8")
return {"response": response,
"audio": audio_b64,
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Run the service if this file is executed as a script
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)