File size: 3,000 Bytes
e95de35
eb66dcb
 
 
e1d7951
eed80a2
e95de35
ef93b5e
e1d7951
 
ef93b5e
eb66dcb
eed80a2
 
 
eb66dcb
 
e1d7951
 
 
 
 
 
 
 
 
eb66dcb
 
 
ef93b5e
 
 
 
 
 
eb66dcb
 
 
ef93b5e
 
 
 
eb66dcb
 
ef93b5e
eb66dcb
 
 
ef93b5e
 
eb66dcb
ef93b5e
5916e02
eb66dcb
 
e95de35
ef93b5e
 
e95de35
ef93b5e
e95de35
 
 
 
 
e1d7951
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException,UploadFile,File
from pydantic import BaseModel
from deep_translator import GoogleTranslator
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import os
from main import process,audio_process
from dotenv import load_dotenv
import base64
from pathlib import Path
load_dotenv()
# Create the FastAPI app instance
os.makedirs("/tmp/huggingface_cache", exist_ok=True)
os.environ["HF_HOME"] = "/tmp/huggingface_cache"

app = FastAPI()

# Allow all CORS (for local testing)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Root endpoint
@app.get("/")
async def home():
    return {"message": "Testing_api"}

# Token verification function
def verify_token(token: str):
    if token != os.getenv("TOKEN"):
        raise HTTPException(status_code=401, detail="Token not matched")

# Translate endpoint that accepts a query parameter 'text'
@app.get("/translate")
async def translate(text: str = "", token: str = ""):
    if not text or not token:
        raise HTTPException(status_code=400, detail="No text or token provided")
    verify_token(token)
    translator = GoogleTranslator(source="auto", target="mr")
    result = translator.translate(text)

    return {"result": result}

@app.get("/chatbot")
async def chatbot(text: str = "", token: str = ""):
    if not text or not token:
        raise HTTPException(status_code=400, detail="No text provided")
    verify_token(token)
    result = process(user_query=text)
    return {"result": result}

@app.post("/audio_chat")
async def audio_chat(audio: UploadFile = File(...), token: str = ""):
    if not audio or not token:
        raise HTTPException(status_code=400, detail="No audio file provided")
    verify_token(token)
    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)}")


UPLOAD_DIR = "uploads"
Path(UPLOAD_DIR).mkdir(exist_ok=True)

# Request model
class FileUploadRequest(BaseModel):
    filename: str
    content_type: str
    base64_file: str

@app.post("/summarizer")
async def upload_base64(file_data: FileUploadRequest):
    try:
        print(file_data.filename)
        file_path = os.path.join(UPLOAD_DIR, file_data.filename)

        # Decode and save file
        with open(file_path, "wb") as f:
            f.write(base64.b64decode(file_data.base64_file))

        # Simulate processing
        extracted_text = f"Saved file: {file_path}\nContent-Type: {file_data.content_type}\n"
        extracted_text += f"(First 100 bytes shown)\n\n"
        with open(file_path, "rb") as f:
            extracted_text += repr(f.read(100))

        return {"text": extracted_text}

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))