Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from helper import generate_reply
|
| 4 |
+
|
| 5 |
+
OPENAI_KEY = os.getenv("OPENAI_API_KEY")
|
| 6 |
+
if not OPENAI_KEY:
|
| 7 |
+
raise ValueError("🔑 Set OPENAI_API_KEY in your Space’s Secrets.")
|
| 8 |
+
|
| 9 |
+
def respond(user_message, history):
|
| 10 |
+
bot_message = generate_reply(user_message)
|
| 11 |
+
history = history or []
|
| 12 |
+
history.append((user_message, bot_message))
|
| 13 |
+
return history, ""
|
| 14 |
+
|
| 15 |
+
with gr.Blocks(css="""
|
| 16 |
+
body { background-color: #f5f5f5; }
|
| 17 |
+
.gradio-container { max-width: 700px; margin: auto; padding: 1rem; }
|
| 18 |
+
""") as demo:
|
| 19 |
+
gr.Markdown("## 🤖 Your AI Companion")
|
| 20 |
+
chatbot = gr.Chatbot()
|
| 21 |
+
with gr.Row():
|
| 22 |
+
txt = gr.Textbox(placeholder="Type here…", show_label=False, lines=1)
|
| 23 |
+
send = gr.Button("Send")
|
| 24 |
+
txt.submit(respond, [txt, chatbot], [chatbot, txt])
|
| 25 |
+
send.click(respond, [txt, chatbot], [chatbot, txt])
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
demo.launch()
|