Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import openai
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import os
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Load environment variables from .env file
|
| 9 |
load_dotenv()
|
|
@@ -14,6 +17,10 @@ openai.api_base = "https://amplifai-openai.openai.azure.com/"
|
|
| 14 |
openai.api_version = "2023-07-01-preview"
|
| 15 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
app = FastAPI()
|
| 18 |
|
| 19 |
class Prompt(BaseModel):
|
|
@@ -178,6 +185,25 @@ def custom_qa_automation(text: str, custom_question: str):
|
|
| 178 |
|
| 179 |
return GenerateResponse(text=formatted_answer)
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
app.add_middleware(
|
| 183 |
CORSMiddleware,
|
|
@@ -206,3 +232,17 @@ def inference(input_prompt: Prompt):
|
|
| 206 |
@app.post("/api/custom-qa", summary="Generate text from prompt", tags=["Generate"], response_model=GenerateResponse)
|
| 207 |
def inference(input_prompt: Prompt, question: Prompt):
|
| 208 |
return custom_qa_automation(text=input_prompt.text, custom_question=question.text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi import HTTPException
|
| 4 |
from pydantic import BaseModel
|
| 5 |
import openai
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import os
|
| 8 |
+
from deepgram import DeepgramClient, PrerecordedOptions
|
| 9 |
+
|
| 10 |
|
| 11 |
# Load environment variables from .env file
|
| 12 |
load_dotenv()
|
|
|
|
| 17 |
openai.api_version = "2023-07-01-preview"
|
| 18 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 19 |
|
| 20 |
+
DEEPGRAM_API_KEY = os.getenv("DEEPGRAM_API_KEY")
|
| 21 |
+
if not DEEPGRAM_API_KEY:
|
| 22 |
+
raise Exception("Deepgram API key not found in environment variables")
|
| 23 |
+
|
| 24 |
app = FastAPI()
|
| 25 |
|
| 26 |
class Prompt(BaseModel):
|
|
|
|
| 185 |
|
| 186 |
return GenerateResponse(text=formatted_answer)
|
| 187 |
|
| 188 |
+
# Transcription function
|
| 189 |
+
def transcription(file):
|
| 190 |
+
try:
|
| 191 |
+
deepgram = DeepgramClient(DEEPGRAM_API_KEY)
|
| 192 |
+
if isinstance(file, str): # File from folder
|
| 193 |
+
with open(file, "rb") as f:
|
| 194 |
+
buffer_data = f.read()
|
| 195 |
+
else: # Uploaded file
|
| 196 |
+
buffer_data = file.file.read()
|
| 197 |
+
|
| 198 |
+
payload = {"buffer": buffer_data}
|
| 199 |
+
options = PrerecordedOptions(model="nova-2", smart_format=True, diarize=True, redact='ssn')
|
| 200 |
+
response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
|
| 201 |
+
return response
|
| 202 |
+
except Exception as e:
|
| 203 |
+
print(f"Exception: {e}")
|
| 204 |
+
return None
|
| 205 |
+
|
| 206 |
+
ALLOWED_EXTENSIONS = {".mp3", ".wav", ".wma"}
|
| 207 |
|
| 208 |
app.add_middleware(
|
| 209 |
CORSMiddleware,
|
|
|
|
| 232 |
@app.post("/api/custom-qa", summary="Generate text from prompt", tags=["Generate"], response_model=GenerateResponse)
|
| 233 |
def inference(input_prompt: Prompt, question: Prompt):
|
| 234 |
return custom_qa_automation(text=input_prompt.text, custom_question=question.text)
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
@app.post("/api/transcribe-audio", summary="Transcribe audio file", tags=["Transcription"])
|
| 238 |
+
async def transcribe_audio_route(file: UploadFile = File(...)):
|
| 239 |
+
# Check if the file extension is allowed
|
| 240 |
+
file_extension = os.path.splitext(file.filename)[1].lower()
|
| 241 |
+
if file_extension not in ALLOWED_EXTENSIONS:
|
| 242 |
+
raise HTTPException(status_code=400, detail=f"File format not supported. Allowed formats: {', '.join(ALLOWED_EXTENSIONS)}")
|
| 243 |
+
|
| 244 |
+
transcription_result = transcription(file)
|
| 245 |
+
if transcription_result:
|
| 246 |
+
return transcription_result
|
| 247 |
+
else:
|
| 248 |
+
return {"error": "Transcription failed"}
|