-
Notifications
You must be signed in to change notification settings - Fork 2
/
tryingback.py
65 lines (55 loc) · 2.34 KB
/
tryingback.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
63
64
65
# main.py (FastAPI)
import random
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from fastapi import HTTPException, Form
from character import Character
import io
import json
import whisper
# this is the open-ai whisper model
# pip install openai-whisper
# sudo apt-get install ffmpeg
# pip install fastapi
class Config(BaseModel):
description: str
class Message(BaseModel):
message: str
app = FastAPI()
character = None
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/backend")
async def receive_config(config_prompts: Config):
"""Listens for POST request on http://127.0.0.1:8000/backend from front-end, then creates a character and returns a placeholder JSON"""
global character
character = Character(config_prompts.description)
return {"message": "response"}
@app.post("/sendMessage")
async def receive_text_json(msg_prompt: Message):
"""Listen for POST request on http://127.0.0.1:8000/sendMessage from front-end containing string prompt, then responds with a JSON containing the response message"""
global character
return {"message": f"{character.chat(msg_prompt.message)}"}
@app.post("/voice")
async def receive_audio_json(audio_blob: dict):
"""Listen for POST request on http://127.0.0.1:8000/voice from front-end containing audio prompt, then responds with a JSON containing the response message"""
# return {"text": "Yo, I'm Peter Quill, also known as Star-Lord. I'm a legendary outlaw who travels the galaxy in search of adventure and fortune. I'm known for my reckless and irresponsible behavior, but I'm also loyal to my friends and always up for a good time."}
try:
# Extract the audio content (adjust the key according to your JSON structure)
audio_content = audio_blob.get("audio")
if not audio_content:
raise HTTPException(status_code=422, detail="Invalid audio data format")
# Load the Whisper model
model = whisper.load_model("tiny.en")
global character
# Transcribe the audio content
result = model.transcribe(audio_content)
return {"text": character.chat(result["text"])}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))