File size: 3,803 Bytes
739f9ff
 
 
4a57254
739f9ff
 
4a57254
739f9ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
982331e
739f9ff
 
982331e
 
 
 
 
 
 
4a57254
 
982331e
739f9ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import glob
from pathlib import Path
import gradio as gr
import nest_asyncio
import dill as pickle

# Ensure async compatibility in Jupyter
nest_asyncio.apply()

# Import OpenAI key with helper function
from helper import get_openai_api_key
OPENAI_API_KEY = get_openai_api_key()

# Define the path to the directory containing the PDF files
folder_path = 'Ehlers-Danlos-1'

# Get the list of all PDF files in the directory
pdf_files = glob.glob(os.path.join(folder_path, '*.pdf'))
print(pdf_files)

# Extract just the filenames (optional)
pdf_filenames = [os.path.basename(pdf) for pdf in pdf_files]
print(pdf_filenames)

# Import utilities
from utils import get_doc_tools

# Truncate function names if necessary
def truncate_function_name(name, max_length=64):
    return name if len(name) <= max_length else name[:max_length]

# Path to save/load serialized tools
tools_cache_path = 'tools_cache.pkl'

# Initialize paper_to_tools_dict
paper_to_tools_dict = {}

# Check if the cache file exists and is not empty
if os.path.exists(tools_cache_path) and os.path.getsize(tools_cache_path) > 0:
    try:
        with open(tools_cache_path, 'rb') as f:
            paper_to_tools_dict = pickle.load(f)
    except EOFError:
        print("Cache file is corrupted. Recreating tools.")
        paper_to_tools_dict = {}
else:
    print("Cache file does not exist or is empty. Recreating tools.")

# Create tools for each PDF if not loaded from cache
if not paper_to_tools_dict:
    for pdf in pdf_files:
        print(f"Getting tools for paper: {pdf}")
        vector_tool, summary_tool = get_doc_tools(pdf, Path(pdf).stem)
        paper_to_tools_dict[pdf] = [vector_tool, summary_tool]
    
    # Save tools to cache
    with open(tools_cache_path, 'wb') as f:
        pickle.dump(paper_to_tools_dict, f)


# Combine all tools into a single list
all_tools = [t for pdf in pdf_files for t in paper_to_tools_dict[pdf]]

# Define an object index and retriever over these tools
from llama_index.core import VectorStoreIndex
from llama_index.core.objects import ObjectIndex

obj_index = ObjectIndex.from_objects(
    all_tools,
    index_cls=VectorStoreIndex,
)

obj_retriever = obj_index.as_retriever(similarity_top_k=3)

# Initialize the OpenAI LLM
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-3.5-turbo")

# Set up the agent
from llama_index.core.agent import FunctionCallingAgentWorker
from llama_index.core.agent import AgentRunner

agent_worker = FunctionCallingAgentWorker.from_tools(
    tool_retriever=obj_retriever,
    llm=llm, 
    verbose=True
)
agent = AgentRunner(agent_worker)

# Define the function to query the agent
def ask_agent(question):
    response = agent.query(question)
    return str(response)

# Create the Gradio interface
iface = gr.Interface(
    fn=ask_agent,
    inputs="text",
    outputs="text",
    title="EDS Research Agent",
)

# Launch the Gradio app
iface.launch(share=True)

"""
import streamlit as st
from transformers import pipeline

# Load your model
generator = pipeline('text-generation', model='gpt-3.5-turbo')

# Streamlit interface
st.title("Text Generator")
prompt = st.text_input("Enter your prompt:")
if st.button("Generate"):
    result = generator(prompt, max_length=50)
    st.write(result[0]['generated_text'])

"""
    
"""
import gradio as gr
from transformers import pipeline

# Load your model
generator = pipeline('text-generation', model='gpt-3.5-turbo')

# Define the function to generate text
def generate_text(prompt):
    result = generator(prompt, max_length=50)
    return result[0]['generated_text']

# Create the Gradio interface
iface = gr.Interface(fn=generate_text, inputs="text", outputs="text", title="Text Generator")

# Launch the interface
iface.launch()
"""
"""
import torch
print(torch.__version__)
"""