File size: 1,570 Bytes
e95de35 eb66dcb e95de35 eb66dcb e95de35 eb66dcb 5916e02 eb66dcb e95de35 |
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 |
from fastapi import FastAPI, HTTPException,UploadFile,File
from pydantic import BaseModel
from deep_translator import GoogleTranslator
from fastapi.responses import JSONResponse
from main import process,audio_process
# Create the FastAPI app instance
app = FastAPI()
# Root endpoint
@app.get("/")
async def home():
return {"message": "Welcome to my FastAPI API on Hugging Face Spaces!"}
# Translate endpoint that accepts a query parameter 'text'
@app.get("/translate")
async def translate(text: str = ""):
if not text:
raise HTTPException(status_code=400, detail="No text provided")
# Perform translation using deep_translator
translator = GoogleTranslator(source="auto", target="mr")
result = translator.translate(text)
return {"result": result}
@app.get("/chatbot")
async def chatbot(text: str = ""):
if not text:
raise HTTPException(status_code=400, detail="No text provided")
# Perform translation using deep_translator
result = process(user_query=text)
return {"result": result}
@app.post("/audio_chat")
async def chatbot(audio: UploadFile = File(...)):
if not audio:
raise HTTPException(status_code=400, detail="No audio file provided")
# Example of processing the audio file (you should replace `process` with your actual function)
try:
result = audio_process(audio.file) # Replace with actual audio processing logic
return {"result": result}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|