test-api / app.py
LalitMahale
catch dir added
eed80a2
raw
history blame
1.69 kB
from fastapi import FastAPI, HTTPException,UploadFile,File
from pydantic import BaseModel
from deep_translator import GoogleTranslator
from fastapi.responses import JSONResponse
import os
from main import process,audio_process
# Create the FastAPI app instance
os.makedirs("/tmp/huggingface_cache", exist_ok=True)
os.environ["HF_HOME"] = "/tmp/huggingface_cache"
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 audio_chat(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)}")