huohguohbo commited on
Commit
0dd0be4
·
1 Parent(s): cc2590a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -38,42 +38,48 @@ def hf_chat(model_name, message):
38
  return bot_response
39
 
40
  # Define the Gradio interface for chatbot
41
- api_key_input = gr.inputs.Textbox(label="OpenAI API Key", default=None)
42
  model_input = gr.inputs.Dropdown(
43
  label="Select OpenAI model",
44
  choices=["davinci", "curie", "babbage"],
45
  default="davinci",
 
46
  )
47
  hf_model_input = gr.inputs.Dropdown(
48
  label="Select Hugging Face model",
49
  choices=["microsoft/DialoGPT-large", "Salesforce/codegen-2B-multi", "microsoft/DialoGPT-small"],
50
  default="microsoft/DialoGPT-large",
 
51
  )
52
  mode_input = gr.inputs.Dropdown(
53
  label="Select chatbot mode",
54
  choices=["OpenAI", "Hugging Face"],
55
  default="OpenAI",
 
56
  )
57
- message_input = gr.inputs.Textbox(label="Enter your message here")
58
- output = gr.outputs.Textbox(label="Bot response")
59
 
60
  # Define the chat window
61
  chat_window = []
62
 
63
- def chatbot(chat_window, message, mode, model, hf_model, api_key):
64
- if message == "/clear":
65
  chat_window.clear()
66
  return "Chat history cleared."
67
- if message:
68
- if mode == "Hugging Face":
69
- bot_response = hf_chat(hf_model, message)
70
- else:
71
- bot_response = openai_chat(api_key, model, message)
72
- chat_window.append(("User", message))
73
- chat_window.append(("Bot", bot_response))
 
74
  return "\n".join([f"{name}: {text}" for name, text in chat_window])
75
 
76
  # Define the Gradio interface for chatbot
 
 
77
  chat_interface = gr.Interface(
78
  fn=chatbot,
79
  inputs=[
@@ -82,8 +88,8 @@ chat_interface = gr.Interface(
82
  model_input,
83
  hf_model_input,
84
  api_key_input,
85
- gr.inputs.Button(label="Send"),
86
- gr.inputs.Button(label="Clear Chat History")
87
  ],
88
  outputs=output,
89
  title="Chatbot",
@@ -92,6 +98,7 @@ chat_interface = gr.Interface(
92
  allow_flagging=False,
93
  allow_screenshot=False,
94
  allow_share=False,
 
95
  )
96
 
97
  # Launch the page
 
38
  return bot_response
39
 
40
  # Define the Gradio interface for chatbot
41
+ api_key_input = gr.inputs.Textbox(label="OpenAI API Key", default=None, block="sidebar")
42
  model_input = gr.inputs.Dropdown(
43
  label="Select OpenAI model",
44
  choices=["davinci", "curie", "babbage"],
45
  default="davinci",
46
+ block="sidebar"
47
  )
48
  hf_model_input = gr.inputs.Dropdown(
49
  label="Select Hugging Face model",
50
  choices=["microsoft/DialoGPT-large", "Salesforce/codegen-2B-multi", "microsoft/DialoGPT-small"],
51
  default="microsoft/DialoGPT-large",
52
+ block="sidebar"
53
  )
54
  mode_input = gr.inputs.Dropdown(
55
  label="Select chatbot mode",
56
  choices=["OpenAI", "Hugging Face"],
57
  default="OpenAI",
58
+ block="sidebar"
59
  )
60
+ message_input = gr.inputs.Textbox(label="Enter your message here", block="input")
61
+ output = gr.outputs.Textbox(label="Bot response", block="output")
62
 
63
  # Define the chat window
64
  chat_window = []
65
 
66
+ def chatbot(chat_window, message, mode, model, hf_model, api_key, send_button, clear_button):
67
+ if clear_button:
68
  chat_window.clear()
69
  return "Chat history cleared."
70
+ if send_button:
71
+ if message:
72
+ if mode == "Hugging Face":
73
+ bot_response = hf_chat(hf_model, message)
74
+ else:
75
+ bot_response = openai_chat(api_key, model, message)
76
+ chat_window.append(("User", message))
77
+ chat_window.append(("Bot", bot_response))
78
  return "\n".join([f"{name}: {text}" for name, text in chat_window])
79
 
80
  # Define the Gradio interface for chatbot
81
+ send_button = gr.inputs.Button(label="Send")
82
+ clear_button = gr.inputs.Button(label="Clear Chat History")
83
  chat_interface = gr.Interface(
84
  fn=chatbot,
85
  inputs=[
 
88
  model_input,
89
  hf_model_input,
90
  api_key_input,
91
+ send_button,
92
+ clear_button
93
  ],
94
  outputs=output,
95
  title="Chatbot",
 
98
  allow_flagging=False,
99
  allow_screenshot=False,
100
  allow_share=False,
101
+ layout="vertical"
102
  )
103
 
104
  # Launch the page