Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Define the chatbot function
|
| 4 |
+
def chat_response(history, user_input):
|
| 5 |
+
"""Handles chatbot interactions."""
|
| 6 |
+
if not user_input.strip():
|
| 7 |
+
return history + [("User", user_input), ("Bot", "Please enter a valid message.")]
|
| 8 |
+
|
| 9 |
+
response = f"You said: {user_input}" # Replace this with actual chatbot logic (e.g., OpenAI API)
|
| 10 |
+
history.append({"role": "user", "content": user_input})
|
| 11 |
+
history.append({"role": "assistant", "content": response})
|
| 12 |
+
|
| 13 |
+
return history, ""
|
| 14 |
+
|
| 15 |
+
# Initialize Gradio chatbot with the correct type
|
| 16 |
+
with gr.Blocks() as demo:
|
| 17 |
+
gr.Markdown("## 🚀 AI Chatbot - Powered by Gradio")
|
| 18 |
+
|
| 19 |
+
chatbot = gr.Chatbot(type="messages") # ✅ Fix: Use 'messages' format
|
| 20 |
+
user_input = gr.Textbox(label="Your Message")
|
| 21 |
+
submit_button = gr.Button("Send")
|
| 22 |
+
|
| 23 |
+
# Button click or Enter key submits the message
|
| 24 |
+
submit_button.click(chat_response, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
|
| 25 |
+
user_input.submit(chat_response, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
|
| 26 |
+
|
| 27 |
+
# Run the Gradio app
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
demo.launch(share=True) # 🚀 Allows sharing via public Gradio link
|