CoolFace
Apppublic

dinhanit/explain_docs

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
app.py51 linesDownload Raw Back to root
1from fastapi import FastAPI2from fastapi import FastAPI, Query3from fastapi import FastAPI, UploadFile4from llm import AskImage5import json,requests6from fastapi.middleware.cors import CORSMiddleware7 8app = FastAPI()9AI = AskImage()10 11 12app.add_middleware(13    CORSMiddleware,14    allow_origins=["*"],15    allow_credentials=True,16    allow_methods=["*"],17    allow_headers=["*"],18)19 20@app.get("/")21async def root():22    return 'Hello'23 24@app.post("/upload_pdf")25async def upload_pdf(pdf_file: UploadFile, userID: str):26    try:27        contents = await pdf_file.read()28        with open(f"storage//{userID}.pdf", "wb") as f:29            f.write(contents)30        return True31    except Exception as e:32        return False33 34@app.get("/upload_pdf_url")35async def upload_pdf(pdf_url: str, userID:str):36    try:37        response = requests.get(pdf_url)38        if response.status_code == 200:39            with open(f"storage//{userID}.pdf", "wb") as f:40                f.write(response.content)41                return True42    except: 43        return False44 45@app.get("/explain_word")46async def upload_pdf(word: str, userID:str):47    try:48        res = AI.explain_word(word,f"storage//{userID}.pdf")49    except Exception as e:50        return f"Error: {e}"51    return res