Spaces:
Runtime error
Runtime error
app2.py
Browse files
app2.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
|
3 |
+
import os
|
4 |
+
import gradio as gr
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import s3fs
|
7 |
+
|
8 |
+
load_dotenv('myenvfile.env')
|
9 |
+
|
10 |
+
os.environ['OPENAI_API_KEY'] = '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'
|
11 |
+
os.environ['AWS_ACCESS_KEY_ID']="AKIAZOU6TJIYU64BCGHE"
|
12 |
+
os.environ['AWS_SECRET_ACCESS_KEY']="RZxYW0WAs53lwdwCXkOo3qCiK7kk5HT+v6deXL7h"
|
13 |
+
from llama_index import GPTListIndex, GPTSimpleVectorIndex
|
14 |
+
from langchain.agents import load_tools, Tool, initialize_agent
|
15 |
+
from langchain.llms import OpenAI
|
16 |
+
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
|
17 |
+
from langchain.agents import initialize_agent, Tool
|
18 |
+
from langchain import OpenAI, LLMChain
|
19 |
+
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader
|
20 |
+
|
21 |
+
|
22 |
+
fs = s3fs.S3FileSystem(
|
23 |
+
key="AKIAZOU6TJIYU64BCGHE",
|
24 |
+
secret="RZxYW0WAs53lwdwCXkOo3qCiK7kk5HT+v6deXL7h"
|
25 |
+
)
|
26 |
+
|
27 |
+
# download the index file from S3
|
28 |
+
with fs.open('notesinendocrinology/index.json', 'rb') as f:
|
29 |
+
index_data = f.read()
|
30 |
+
|
31 |
+
import json
|
32 |
+
|
33 |
+
# Decode the bytes to a string using UTF-8 encoding
|
34 |
+
index_str = index_data.decode('utf-8')
|
35 |
+
|
36 |
+
# Load the JSON data into a dictionary
|
37 |
+
index_dict = json.loads(index_str)
|
38 |
+
|
39 |
+
# create a new index object and load the saved index from the downloaded file
|
40 |
+
index = GPTSimpleVectorIndex.load_from_string(index_str)
|
41 |
+
|
42 |
+
|
43 |
+
def querying_db(query: str):
|
44 |
+
response = index.query(query)
|
45 |
+
return response
|
46 |
+
|
47 |
+
|
48 |
+
tools = [
|
49 |
+
Tool(
|
50 |
+
name="QueryingDB",
|
51 |
+
func=querying_db,
|
52 |
+
description="This function takes a query string as input and returns the most relevant answer from the documentation as output"
|
53 |
+
)
|
54 |
+
]
|
55 |
+
|
56 |
+
llm = OpenAI(temperature=0)
|
57 |
+
|
58 |
+
|
59 |
+
def get_answer(query_string):
|
60 |
+
agent = initialize_agent(tools, llm, agent="zero-shot-react-description")
|
61 |
+
result = agent.run(query_string)
|
62 |
+
return result
|
63 |
+
|
64 |
+
|
65 |
+
def qa_app(query):
|
66 |
+
answer = get_answer(query)
|
67 |
+
return answer
|
68 |
+
|
69 |
+
|
70 |
+
inputs = gr.inputs.Textbox(label="Enter your question:")
|
71 |
+
output = gr.outputs.Textbox(label="Answer:")
|
72 |
+
gr.Interface(fn=qa_app, inputs=inputs, outputs=output, title="Query Answering App").launch()
|