Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,21 @@
|
|
|
|
|
|
|
|
| 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 |
-
|
| 33 |
-
# Securely retrieve the OpenAI API key
|
| 34 |
-
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 35 |
-
import os
|
| 36 |
-
|
| 37 |
-
# Securely retrieve the OpenAI API key from the environment variable
|
| 38 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 39 |
|
| 40 |
-
if not openai_api_key:
|
| 41 |
-
raise ValueError("Missing OpenAI API key. Set OPENAI_API_KEY in your environment variables.")
|
| 42 |
-
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 43 |
-
if openai_api_key:
|
| 44 |
-
print("API Key loaded successfully!")
|
| 45 |
-
else:
|
| 46 |
-
print("API Key not found.")
|
| 47 |
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 51 |
|
| 52 |
-
# Define the prompt template
|
| 53 |
prompt_template = """
|
| 54 |
You are an expert in skin cancer, etc.
|
| 55 |
Answer the question based only on the following context, which can include text, images, and tables:
|
|
@@ -63,15 +29,10 @@ Just return the helpful answer in as much detail as possible.
|
|
| 63 |
Answer:
|
| 64 |
"""
|
| 65 |
|
| 66 |
-
qa_chain = LLMChain(llm=ChatOpenAI(model="gpt-4", openai_api_key
|
| 67 |
prompt=PromptTemplate.from_template(prompt_template))
|
| 68 |
|
| 69 |
-
|
| 70 |
-
async def index(request: Request):
|
| 71 |
-
return templates.TemplateResponse("index.html", {"request": request})
|
| 72 |
-
|
| 73 |
-
@app.post("/get_answer")
|
| 74 |
-
async def get_answer(question: str = Form(...)):
|
| 75 |
relevant_docs = db.similarity_search(question)
|
| 76 |
context = ""
|
| 77 |
relevant_images = []
|
|
@@ -84,4 +45,10 @@ async def get_answer(question: str = Form(...)):
|
|
| 84 |
context += '[image]' + d.page_content
|
| 85 |
relevant_images.append(d.metadata['original_content'])
|
| 86 |
result = qa_chain.run({'context': context, 'question': question})
|
| 87 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
from langchain.chat_models import ChatOpenAI
|
| 4 |
from langchain.embeddings import OpenAIEmbeddings
|
| 5 |
from langchain.chains import LLMChain
|
| 6 |
from langchain.prompts import PromptTemplate
|
|
|
|
|
|
|
| 7 |
from langchain.vectorstores import FAISS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
|
|
|
|
|
|
| 17 |
db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 18 |
|
|
|
|
| 19 |
prompt_template = """
|
| 20 |
You are an expert in skin cancer, etc.
|
| 21 |
Answer the question based only on the following context, which can include text, images, and tables:
|
|
|
|
| 29 |
Answer:
|
| 30 |
"""
|
| 31 |
|
| 32 |
+
qa_chain = LLMChain(llm=ChatOpenAI(model="gpt-4", openai_api_key=openai_api_key, max_tokens=1024),
|
| 33 |
prompt=PromptTemplate.from_template(prompt_template))
|
| 34 |
|
| 35 |
+
def get_answer(question: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
relevant_docs = db.similarity_search(question)
|
| 37 |
context = ""
|
| 38 |
relevant_images = []
|
|
|
|
| 45 |
context += '[image]' + d.page_content
|
| 46 |
relevant_images.append(d.metadata['original_content'])
|
| 47 |
result = qa_chain.run({'context': context, 'question': question})
|
| 48 |
+
return {"relevant_images": relevant_images[0], "result": result}
|
| 49 |
+
|
| 50 |
+
iface = gr.Interface(fn=get_answer, inputs="text", outputs="json")
|
| 51 |
+
|
| 52 |
+
# Run the Gradio interface inside FastAPI
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
iface.launch()
|