LalitMahale
commited on
Commit
·
e1d7951
1
Parent(s):
1557dff
model name and new api added
Browse files- app.py +43 -1
- utils/rag.py +1 -1
app.py
CHANGED
|
@@ -2,17 +2,28 @@ from fastapi import FastAPI, HTTPException,UploadFile,File
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from deep_translator import GoogleTranslator
|
| 4 |
from fastapi.responses import JSONResponse
|
|
|
|
| 5 |
import os
|
| 6 |
from main import process,audio_process
|
| 7 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
| 8 |
load_dotenv()
|
| 9 |
# Create the FastAPI app instance
|
| 10 |
os.makedirs("/tmp/huggingface_cache", exist_ok=True)
|
| 11 |
os.environ["HF_HOME"] = "/tmp/huggingface_cache"
|
| 12 |
|
| 13 |
-
|
| 14 |
app = FastAPI()
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Root endpoint
|
| 17 |
@app.get("/")
|
| 18 |
async def home():
|
|
@@ -52,3 +63,34 @@ async def audio_chat(audio: UploadFile = File(...), token: str = ""):
|
|
| 52 |
return {"result": result}
|
| 53 |
except Exception as e:
|
| 54 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from deep_translator import GoogleTranslator
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
import os
|
| 7 |
from main import process,audio_process
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
+
import base64
|
| 10 |
+
from pathlib import Path
|
| 11 |
load_dotenv()
|
| 12 |
# Create the FastAPI app instance
|
| 13 |
os.makedirs("/tmp/huggingface_cache", exist_ok=True)
|
| 14 |
os.environ["HF_HOME"] = "/tmp/huggingface_cache"
|
| 15 |
|
|
|
|
| 16 |
app = FastAPI()
|
| 17 |
|
| 18 |
+
# Allow all CORS (for local testing)
|
| 19 |
+
app.add_middleware(
|
| 20 |
+
CORSMiddleware,
|
| 21 |
+
allow_origins=["*"],
|
| 22 |
+
allow_credentials=True,
|
| 23 |
+
allow_methods=["*"],
|
| 24 |
+
allow_headers=["*"],
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
# Root endpoint
|
| 28 |
@app.get("/")
|
| 29 |
async def home():
|
|
|
|
| 63 |
return {"result": result}
|
| 64 |
except Exception as e:
|
| 65 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
UPLOAD_DIR = "uploads"
|
| 69 |
+
Path(UPLOAD_DIR).mkdir(exist_ok=True)
|
| 70 |
+
|
| 71 |
+
# Request model
|
| 72 |
+
class FileUploadRequest(BaseModel):
|
| 73 |
+
filename: str
|
| 74 |
+
content_type: str
|
| 75 |
+
base64_file: str
|
| 76 |
+
|
| 77 |
+
@app.post("/summarizer")
|
| 78 |
+
async def upload_base64(file_data: FileUploadRequest):
|
| 79 |
+
try:
|
| 80 |
+
print(file_data.filename)
|
| 81 |
+
file_path = os.path.join(UPLOAD_DIR, file_data.filename)
|
| 82 |
+
|
| 83 |
+
# Decode and save file
|
| 84 |
+
with open(file_path, "wb") as f:
|
| 85 |
+
f.write(base64.b64decode(file_data.base64_file))
|
| 86 |
+
|
| 87 |
+
# Simulate processing
|
| 88 |
+
extracted_text = f"Saved file: {file_path}\nContent-Type: {file_data.content_type}\n"
|
| 89 |
+
extracted_text += f"(First 100 bytes shown)\n\n"
|
| 90 |
+
with open(file_path, "rb") as f:
|
| 91 |
+
extracted_text += repr(f.read(100))
|
| 92 |
+
|
| 93 |
+
return {"text": extracted_text}
|
| 94 |
+
|
| 95 |
+
except Exception as e:
|
| 96 |
+
raise HTTPException(status_code=500, detail=str(e))
|
utils/rag.py
CHANGED
|
@@ -9,7 +9,7 @@ load_dotenv()
|
|
| 9 |
class RAG:
|
| 10 |
def __init__(self):
|
| 11 |
self.url = 'https://lalitmahale.github.io'
|
| 12 |
-
self.llm = GoogleGenerativeAI(google_api_key=os.getenv("GOOGLE_API"),model="gemini-pro")
|
| 13 |
|
| 14 |
|
| 15 |
def get_data(self):
|
|
|
|
| 9 |
class RAG:
|
| 10 |
def __init__(self):
|
| 11 |
self.url = 'https://lalitmahale.github.io'
|
| 12 |
+
self.llm = GoogleGenerativeAI(google_api_key=os.getenv("GOOGLE_API"),model="gemini-1.5-pro")
|
| 13 |
|
| 14 |
|
| 15 |
def get_data(self):
|