rohan13 commited on
Commit
bdfad5e
1 Parent(s): 4a443ee

Gradio bot for QA on apis

Browse files
Files changed (3) hide show
  1. app.py +43 -0
  2. main.py +47 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from main import chat, index_url
3
+
4
+ """Use text to call chat method from main.py"""
5
+ def add_text(history, text):
6
+ response = str(chat(question=text))
7
+ history = history + [(text, response)]
8
+ return history, ""
9
+
10
+ """Use text to call index_url method from main.py"""
11
+ def add_url(history, url):
12
+ data = index_url(url)
13
+ response = "Found your data!"
14
+ history = history + [(url, response)]
15
+ return history, ""
16
+
17
+ def bot(history):
18
+ return history
19
+
20
+ with gr.Blocks() as demo:
21
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
22
+
23
+ with gr.Row():
24
+ with gr.Column(scale=0.65):
25
+ txt = gr.Textbox(
26
+ show_label=False,
27
+ placeholder="Enter text and press enter, or upload an image",
28
+ ).style(container=False)
29
+ with gr.Column(scale=0.35, min_width=0):
30
+ url = gr.Textbox(
31
+ show_label=False,
32
+ placeholder="Enter url api endpoint",
33
+ ).style(container=False)
34
+
35
+ txt.submit(add_text, [chatbot, txt], [chatbot, txt]).then(
36
+ bot, chatbot, chatbot
37
+ )
38
+ url.submit(add_url, [chatbot, url], [chatbot, url]).then(
39
+ bot, chatbot, chatbot
40
+ )
41
+
42
+ if __name__ == "__main__":
43
+ demo.launch()
main.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import requests
4
+ import yaml
5
+ from langchain.agents import (
6
+ create_json_agent
7
+ )
8
+ from langchain.agents.agent_toolkits import JsonToolkit
9
+ from langchain.chat_models import ChatOpenAI
10
+ from langchain.tools.json.tool import JsonSpec
11
+
12
+ os.environ['OPENAI_API_KEY'] = "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"
13
+
14
+ llm = ChatOpenAI(model_name="gpt-4", temperature=0.1)
15
+ agent = None
16
+
17
+ """Call the url and use json response to create a yaml file"""
18
+ def index_url(url):
19
+ global agent
20
+ response = requests.get(url)
21
+ json_response = response.json()
22
+
23
+ # create yaml from json response
24
+ yaml_string = yaml.dump(json_response)
25
+
26
+ data = yaml.load(yaml_string, Loader=yaml.FullLoader)
27
+ json_spec = JsonSpec(dict_=data, max_value_length=4000)
28
+ json_toolkit = JsonToolkit(spec=json_spec)
29
+ agent = create_json_agent(
30
+ llm=llm,
31
+ toolkit=json_toolkit,
32
+ verbose=True
33
+ )
34
+ return data
35
+
36
+ """Use the agent to call method run using the text as input"""
37
+ def chat(question):
38
+ global agent
39
+ if agent is not None:
40
+ response = agent.run(question)
41
+ print(response)
42
+ else:
43
+ response = "Please add a request url first"
44
+ return response
45
+
46
+
47
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ langchain
3
+ openai