|
from fastapi import FastAPI, HTTPException,UploadFile,File,Request |
|
from pydantic import BaseModel |
|
from deep_translator import GoogleTranslator |
|
from fastapi.responses import JSONResponse |
|
from fastapi.middleware.cors import CORSMiddleware |
|
import os |
|
from text import fitness_role |
|
|
|
from dotenv import load_dotenv |
|
import base64 |
|
from pathlib import Path |
|
from process import Response |
|
import uuid |
|
load_dotenv() |
|
|
|
os.makedirs("/tmp/huggingface_cache", exist_ok=True) |
|
os.environ["HF_HOME"] = "/tmp/huggingface_cache" |
|
|
|
app = FastAPI() |
|
|
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["*"], |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
uploaded_docs = {} |
|
|
|
class UploadRequest(BaseModel): |
|
filename: str |
|
filedata: str |
|
|
|
class ChatRequest(BaseModel): |
|
session_id: str |
|
message: str |
|
|
|
class ChatBot(BaseModel): |
|
text :str |
|
token:str |
|
|
|
|
|
@app.get("/") |
|
async def home(): |
|
return {"message": "Test ok"} |
|
|
|
|
|
def verify_token(token: str): |
|
print("token: ",token) |
|
if token != os.getenv("TOKEN"): |
|
raise HTTPException(status_code=401, detail="Token not matched") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/chatbot") |
|
async def chatbot(req:ChatBot): |
|
try: |
|
query = req.text |
|
token = req.token |
|
|
|
if not query or not token: |
|
raise HTTPException(status_code=400, detail="No text provided") |
|
verify_token(token=token) |
|
res = Response().chatbot(query=query) |
|
|
|
return {"result":res} |
|
except Exception as e: |
|
print(e) |
|
|
|
@app.post("/fitnessbot") |
|
async def fitnessbot(req:ChatBot): |
|
try: |
|
query = req.text |
|
token = req.token |
|
|
|
if not query or not token: |
|
raise HTTPException(status_code=400, detail="No text provided") |
|
verify_token(token=token) |
|
res = Response().other_chatbot(query=query,role = fitness_role) |
|
|
|
return {"result":res} |
|
except Exception as e: |
|
print(e) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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) |
|
return {"result": result} |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") |
|
|
|
|
|
|
|
class FileUploadRequest(BaseModel): |
|
filename: str |
|
content_type: str |
|
base64_file: str |
|
|
|
UPLOAD_DIR = "/tmp/uploads" |
|
Path(UPLOAD_DIR).mkdir(parents=True, exist_ok=True) |
|
|
|
@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) |
|
|
|
with open(file_path, "wb") as f: |
|
f.write(base64.b64decode(file_data.base64_file)) |
|
|
|
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": "api under development"} |
|
|
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
@app.post("/upload") |
|
async def upload_file(req: UploadRequest): |
|
session_id = str(uuid.uuid4()) |
|
decoded_data = base64.b64decode(req.filedata) |
|
|
|
uploaded_docs[session_id] = decoded_data |
|
print(session_id) |
|
return {"success": True, "session_id": session_id} |
|
|
|
@app.post("/chat") |
|
async def chat(req: ChatRequest): |
|
pdf_data = uploaded_docs.get(req.session_id) |
|
if not pdf_data: |
|
return {"reply": "Session not found."} |
|
|
|
|
|
dummy_answer = f" Nice Mocked answer for: '{req.message}'" |
|
return {"reply": dummy_answer} |
|
|