hayuh commited on
Commit
7628860
·
verified ·
1 Parent(s): a1162e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -59
app.py CHANGED
@@ -1,63 +1,83 @@
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
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
- if __name__ == "__main__":
63
- demo.launch()
 
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()