| 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 |
| |
| os.makedirs("/tmp/huggingface_cache", exist_ok=True) |
| os.environ["HF_HOME"] = "/tmp/huggingface_cache" |
|
|
|
|
| app = FastAPI() |
|
|
|
|
| |
| @app.get("/") |
| async def home(): |
| return {"message": "Welcome to my FastAPI API on Hugging Face Spaces!"} |
|
|
| |
| @app.get("/translate") |
| async def translate(text: str = ""): |
| if not text: |
| raise HTTPException(status_code=400, detail="No text provided") |
| |
| |
| 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") |
| |
| 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") |
|
|
| |
| try: |
| result = audio_process(audio.file) |
| return {"result": result} |
| except Exception as e: |
| raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") |
|
|