Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.chat_models import ChatOpenAI
|
2 |
+
from langchain.embeddings import OpenAIEmbeddings
|
3 |
+
from langchain.chains import LLMChain
|
4 |
+
from langchain.prompts import PromptTemplate
|
5 |
+
from langchain.schema.messages import HumanMessage, SystemMessage
|
6 |
+
from langchain.schema.document import Document
|
7 |
+
from langchain.vectorstores import FAISS
|
8 |
+
from langchain.retrievers.multi_vector import MultiVectorRetriever
|
9 |
+
import os
|
10 |
+
import uuid
|
11 |
+
import base64
|
12 |
+
from fastapi import FastAPI, Request, Form, Response, File, UploadFile
|
13 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
14 |
+
from fastapi.templating import Jinja2Templates
|
15 |
+
from fastapi.encoders import jsonable_encoder
|
16 |
+
from fastapi.middleware.cors import CORSMiddleware
|
17 |
+
import json
|
18 |
+
from dotenv import load_dotenv
|
19 |
+
load_dotenv()
|
20 |
+
|
21 |
+
app = FastAPI()
|
22 |
+
templates = Jinja2Templates(directory="templates")
|
23 |
+
|
24 |
+
# Configure CORS
|
25 |
+
app.add_middleware(
|
26 |
+
CORSMiddleware,
|
27 |
+
allow_origins=["*"],
|
28 |
+
allow_credentials=True,
|
29 |
+
allow_methods=["*"],
|
30 |
+
allow_headers=["*"],
|
31 |
+
)
|
32 |
+
openai_api_key = os.getenv("apikey") # Replace with your actual API key
|
33 |
+
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
34 |
+
|
35 |
+
|
36 |
+
db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
37 |
+
|
38 |
+
# Define the prompt template
|
39 |
+
prompt_template = """
|
40 |
+
You are an expert in skin cancer, etc.
|
41 |
+
Answer the question based only on the following context, which can include text, images, and tables:
|
42 |
+
{context}
|
43 |
+
Question: {question}
|
44 |
+
Don't answer if you are not sure and decline to answer and say "Sorry, I don't have much information about it."
|
45 |
+
Just return the helpful answer in as much detail as possible.
|
46 |
+
Answer:
|
47 |
+
"""
|
48 |
+
|
49 |
+
qa_chain = LLMChain(llm=ChatOpenAI(model="gpt-4", openai_api_key = openai_api_key, max_tokens=1024),
|
50 |
+
prompt=PromptTemplate.from_template(prompt_template))
|
51 |
+
|
52 |
+
@app.get("/", response_class=HTMLResponse)
|
53 |
+
async def index(request: Request):
|
54 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
55 |
+
|
56 |
+
@app.post("/get_answer")
|
57 |
+
async def get_answer(question: str = Form(...)):
|
58 |
+
relevant_docs = db.similarity_search(question)
|
59 |
+
context = ""
|
60 |
+
relevant_images = []
|
61 |
+
for d in relevant_docs:
|
62 |
+
if d.metadata['type'] == 'text':
|
63 |
+
context += '[text]' + d.metadata['original_content']
|
64 |
+
elif d.metadata['type'] == 'table':
|
65 |
+
context += '[table]' + d.metadata['original_content']
|
66 |
+
elif d.metadata['type'] == 'image':
|
67 |
+
context += '[image]' + d.page_content
|
68 |
+
relevant_images.append(d.metadata['original_content'])
|
69 |
+
result = qa_chain.run({'context': context, 'question': question})
|
70 |
+
return JSONResponse({"relevant_images": relevant_images[0], "result": result})
|