ainur1 commited on
Commit
afa6fc0
·
1 Parent(s): 79d7621
Files changed (1) hide show
  1. app.py +32 -52
app.py CHANGED
@@ -1,55 +1,35 @@
1
  import gradio as gr
2
  from utils import Bot
3
 
4
- # Define the initialization function for the bot
5
- def initialize_bot(openai_api_key, table_descriptions, text_documents):
6
- global bot
7
- bot = Bot(openai_api_key=openai_api_key, table_descriptions=table_descriptions, text_documents=text_documents, verbose=True)
8
-
9
- # Define the function to generate the bot's response
10
- def generate_response(question):
11
- response = bot(question)
12
- return response
13
-
14
- # Define the interface components
15
- openai_api_key_input = gr.inputs.Textbox(label="OpenAI API Key")
16
- table_descriptions_input = gr.inputs.Textbox(label="Table Descriptions")
17
- text_documents_input = gr.inputs.Textbox(label="Text Documents")
18
-
19
- submit_button = gr.outputs.Button(label="Submit")
20
-
21
- question_input = gr.inputs.Textbox(label="Ask a question")
22
- submit_question_button = gr.outputs.Button(label="Submit")
23
-
24
- # Define the interface layout
25
- input_components = [
26
- openai_api_key_input,
27
- table_descriptions_input,
28
- text_documents_input,
29
- submit_button,
30
- ]
31
-
32
- output_components = [
33
- question_input,
34
- submit_question_button,
35
- ]
36
-
37
- interface = gr.Interface(
38
- initialize_bot,
39
- input_components,
40
- show_input=False,
41
- outputs=output_components,
42
- title="Chat with your Bot",
43
- )
44
-
45
- # Define the callback function for the submit button
46
- def submit_callback(openai_api_key, table_descriptions, text_documents):
47
- initialize_bot(openai_api_key, table_descriptions, text_documents)
48
-
49
- # Define the callback function for the submit question button
50
- def submit_question_callback(question):
51
- response = generate_response(question)
52
- return response
53
-
54
- # Add the callbacks to the interface
55
- interface.launch(inline=False, submit_button=submit_callback, submit_question_button=submit_question_callback)
 
1
  import gradio as gr
2
  from utils import Bot
3
 
4
+ def chatbot_interface():
5
+ openai_api_key = gr.inputs.Textbox(label="OpenAI API Key")
6
+ table_descriptions = gr.inputs.Textbox(label="Table Descriptions")
7
+ text_documents = gr.inputs.Textbox(label="Text Documents")
8
+ init_inputs = [openai_api_key, table_descriptions, text_documents]
9
+
10
+ def init_bot(openai_api_key, table_descriptions, text_documents):
11
+ bot = Bot(openai_api_key=openai_api_key, table_descriptions=table_descriptions, text_documents=text_documents)
12
+ return bot
13
+
14
+ chatbot = gr.inputs.Textbox(label="Ask a question")
15
+ submit_init_button = gr.Interface(
16
+ fn=init_bot,
17
+ inputs=init_inputs,
18
+ outputs=None,
19
+ title="Chat with your Bot",
20
+ description="Enter initialization parameters and click submit to start chatting with your bot."
21
+ )
22
+ submit_chat_button = gr.Interface(
23
+ fn=lambda bot, question: bot(question),
24
+ inputs=[gr.inputs.Hidden(), chatbot],
25
+ outputs=gr.outputs.Textbox(label="Bot Response"),
26
+ title="Chat with your Bot",
27
+ description="Type a question and click submit to get a response from your bot."
28
+ )
29
+
30
+ return submit_init_button, submit_chat_button
31
+
32
+ submit_init_button, submit_chat_button = chatbot_interface()
33
+
34
+ submit_init_button.launch()
35
+ submit_chat_button.launch()