-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
100 lines (85 loc) · 2.98 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from pydub import AudioSegment
import io
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi import HTTPException, status, UploadFile
from app import helpers
import os
import os.path
import openai
# from mangum import Mangum
from fastapi.middleware.cors import CORSMiddleware
from app.helpers import OPENAI_API_KEY
openai.api_key = OPENAI_API_KEY
app = FastAPI()
# handler = Mangum(app)
origins = [
"*"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# @app.post("/api/generate/",
@app.post("/api/generate/chat/",
tags=["ChatBot Grievance"],
description="Generate Response from Grievance Chatbot")
async def generate_response(text:str):
try:
res = helpers.chatbotQA(text)
return {"response":res}
except:
return HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
@app.post("/api/generate/submit/",
tags=["ChatBot Grievance"],
description="Generate Response from Grievance Chatbot")
async def generate_response(text:str):
try:
res = helpers.chatbotQASubmit(text)
return {"response":res}
except:
return HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
@app.post("/api/index/",
tags=["ChatBot Grievance"],
description="Create Index with Grievance Chatbot",)
async def create_index(text:str,file:str):
try:
current_directory = os.path.dirname(__file__)
file_path = os.path.join(current_directory, "data_grieviance/"+file+".txt")
with open(file_path, 'a') as file:
file.write("\n"+text)
res = helpers.ConstructIndex("./data_grieviance/")
return {"index updated successfully"}
except:
return HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
@app.get("/api/token/",
tags=["ChatBot Grievance"],
description="Count tokens for Chat History",)
async def count_token(text:str):
try:
res = helpers.count_tokens(text)
return {"tokens":res}
except:
return HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
@app.post("/api/transcribe/",
tags=["ChatBot Grievance"],
description="Transcribe Speech to Text and Generate Response")
async def transcribe_and_generate_response(audio_file: UploadFile):
audio = audio_file.file.read()
buffer = io.BytesIO(audio)
buffer.name = audio_file.filename
# Transcribe audio using the OpenAI Whisper API
response = openai.Audio.translate(
api_key=OPENAI_API_KEY,
model="whisper-1",
file=buffer
)
# Get the transcribed text from the API response
transcribed_text = response["text"]
print(transcribed_text)
# Now, pass the transcribed text to the "generate_response" endpoint
# res = await generate_response(transcribed_text)
return transcribed_text