Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
respond,
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import glob
|
| 3 |
+
from pathlib import Path
|
| 4 |
import gradio as gr
|
| 5 |
+
import nest_asyncio
|
| 6 |
+
|
| 7 |
+
# Ensure async compatibility in Jupyter
|
| 8 |
+
nest_asyncio.apply()
|
| 9 |
+
|
| 10 |
+
# Import OpenAI key with helper function
|
| 11 |
+
from helper import get_openai_api_key
|
| 12 |
+
OPENAI_API_KEY = get_openai_api_key()
|
| 13 |
+
|
| 14 |
+
# Define the path to the directory containing the PDF files
|
| 15 |
+
folder_path = 'Ehlers-Danlos-1'
|
| 16 |
+
|
| 17 |
+
# Get the list of all PDF files in the directory
|
| 18 |
+
pdf_files = glob.glob(os.path.join(folder_path, '*.pdf'))
|
| 19 |
+
print(pdf_files)
|
| 20 |
+
|
| 21 |
+
# Extract just the filenames (optional)
|
| 22 |
+
pdf_filenames = [os.path.basename(pdf) for pdf in pdf_files]
|
| 23 |
+
print(pdf_filenames)
|
| 24 |
+
|
| 25 |
+
# Import utilities
|
| 26 |
+
from utils import get_doc_tools
|
| 27 |
+
|
| 28 |
+
# Truncate function names if necessary
|
| 29 |
+
def truncate_function_name(name, max_length=64):
|
| 30 |
+
return name if len(name) <= max_length else name[:max_length]
|
| 31 |
+
|
| 32 |
+
# Create tools for each PDF
|
| 33 |
+
paper_to_tools_dict = {}
|
| 34 |
+
for pdf in pdf_files:
|
| 35 |
+
print(f"Getting tools for paper: {pdf}")
|
| 36 |
+
vector_tool, summary_tool = get_doc_tools(pdf, Path(pdf).stem)
|
| 37 |
+
paper_to_tools_dict[pdf] = [vector_tool, summary_tool]
|
| 38 |
+
|
| 39 |
+
# Combine all tools into a single list
|
| 40 |
+
all_tools = [t for pdf in pdf_files for t in paper_to_tools_dict[pdf]]
|
| 41 |
+
|
| 42 |
+
# Define an object index and retriever over these tools
|
| 43 |
+
from llama_index.core import VectorStoreIndex
|
| 44 |
+
from llama_index.core.objects import ObjectIndex
|
| 45 |
+
|
| 46 |
+
obj_index = ObjectIndex.from_objects(
|
| 47 |
+
all_tools,
|
| 48 |
+
index_cls=VectorStoreIndex,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
+
obj_retriever = obj_index.as_retriever(similarity_top_k=3)
|
| 52 |
+
|
| 53 |
+
# Initialize the OpenAI LLM
|
| 54 |
+
from llama_index.llms.openai import OpenAI
|
| 55 |
+
llm = OpenAI(model="gpt-3.5-turbo")
|
| 56 |
+
|
| 57 |
+
# Set up the agent
|
| 58 |
+
from llama_index.core.agent import FunctionCallingAgentWorker
|
| 59 |
+
from llama_index.core.agent import AgentRunner
|
| 60 |
+
|
| 61 |
+
agent_worker = FunctionCallingAgentWorker.from_tools(
|
| 62 |
+
tool_retriever=obj_retriever,
|
| 63 |
+
llm=llm,
|
| 64 |
+
verbose=True
|
| 65 |
+
)
|
| 66 |
+
agent = AgentRunner(agent_worker)
|
| 67 |
+
|
| 68 |
+
# Define the function to query the agent
|
| 69 |
+
def ask_agent(question):
|
| 70 |
+
response = agent.query(question)
|
| 71 |
+
return str(response)
|
| 72 |
+
|
| 73 |
+
# Create the Gradio interface
|
| 74 |
+
iface = gr.Interface(
|
| 75 |
+
fn=ask_agent,
|
| 76 |
+
inputs="text",
|
| 77 |
+
outputs="text",
|
| 78 |
+
title="EDS Diagnosis Helper",
|
| 79 |
+
description="Ask questions related to Ehlers-Danlos Syndrome diagnosis.",
|
| 80 |
+
)
|
| 81 |
|
| 82 |
+
# Launch the Gradio app
|
| 83 |
+
iface.launch()
|