huohguohbo commited on
Commit
0a278b8
·
1 Parent(s): 7f8eac7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -9
app.py CHANGED
@@ -57,9 +57,26 @@ mode_input = gr.inputs.Dropdown(
57
  message_input = gr.inputs.Textbox(label="Enter your message here")
58
  output = gr.outputs.Textbox(label="Bot response")
59
 
60
- chat_button = gr.Interface(
61
- fn=lambda message, model, hf_model, api_key, mode: hf_chat(hf_model, message) if mode == "Hugging Face" else openai_chat(api_key, model, message),
62
- inputs=[message_input, model_input, hf_model_input, api_key_input, mode_input],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  outputs=output,
64
  title="Chatbot",
65
  description="Enter your message below to chat with an AI",
@@ -67,11 +84,29 @@ chat_button = gr.Interface(
67
  allow_flagging=False,
68
  allow_screenshot=False,
69
  allow_share=False,
70
- examples=[
71
- ["Hello, how are you?", "", "", None, "OpenAI"],
72
- ["What's the weather like today?", "", "", None, "Hugging Face"],
73
- ["Can you help me with some Python code?", "```python\nfor i in range(10):\n print(i)\n```", "", None, "OpenAI"],
74
- ],
75
  )
76
 
77
- chat_button.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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=[message_input, mode_input, model_input, hf_model_input, api_key_input],
80
  outputs=output,
81
  title="Chatbot",
82
  description="Enter your message below to chat with an AI",
 
84
  allow_flagging=False,
85
  allow_screenshot=False,
86
  allow_share=False,
 
 
 
 
 
87
  )
88
 
89
+ # Add a clear button to the chat window
90
+ clear_button = gr.Interface(
91
+ fn=lambda: chat_window.clear(),
92
+ inputs=None,
93
+ outputs=gr.outputs.Textbox(label="Chat history cleared."),
94
+ title="Clear Chat History",
95
+ description="Click to clear the chat history.",
96
+ theme="compact",
97
+ allow_flagging=False,
98
+ allow_screenshot=False,
99
+ allow_share=False,
100
+ )
101
+
102
+ # Combine the chat interface and clear button into a single page
103
+ page = gr.Interface(
104
+ [chat_interface, clear_button],
105
+ title="Chatbot",
106
+ description="Enter your message below to chat with an AI",
107
+ theme="compact",
108
+ layout="horizontal",
109
+ )
110
+
111
+ # Launch the page
112
+ page.launch()