Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Load available memes/gifs from a folder
|
| 6 |
+
MEME_FOLDER = "assets/memes"
|
| 7 |
+
memes = [os.path.join(MEME_FOLDER, f) for f in os.listdir(MEME_FOLDER) if f.endswith((".gif", ".jpg", ".png"))]
|
| 8 |
+
|
| 9 |
+
chat_history = []
|
| 10 |
+
|
| 11 |
+
def send_message(user_message, include_meme):
|
| 12 |
+
if not user_message.strip():
|
| 13 |
+
return chat_history, "Please enter a message before sending."
|
| 14 |
+
|
| 15 |
+
# Append user's message
|
| 16 |
+
chat_history.append(("You", user_message))
|
| 17 |
+
|
| 18 |
+
# Simulate contact reply (placeholder)
|
| 19 |
+
reply = f"Message sent to your contact: {user_message}"
|
| 20 |
+
chat_history.append(("Contact", reply))
|
| 21 |
+
|
| 22 |
+
# Add meme if selected
|
| 23 |
+
meme_output = None
|
| 24 |
+
if include_meme and memes:
|
| 25 |
+
selected_meme = random.choice(memes)
|
| 26 |
+
chat_history.append(("Meme", selected_meme))
|
| 27 |
+
meme_output = selected_meme
|
| 28 |
+
|
| 29 |
+
return chat_history, ""
|
| 30 |
+
|
| 31 |
+
def format_chat(chat):
|
| 32 |
+
display = ""
|
| 33 |
+
for sender, message in chat:
|
| 34 |
+
if sender == "Meme":
|
| 35 |
+
display += f"<img src='file/{message}' width='200px'><br>"
|
| 36 |
+
else:
|
| 37 |
+
display += f"<b>{sender}:</b> {message}<br>"
|
| 38 |
+
return display
|
| 39 |
+
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("# 💬 Chat Box App with Memes and GIFs")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column():
|
| 45 |
+
msg_input = gr.Textbox(placeholder="Type your message here...", label="Your Message")
|
| 46 |
+
include_meme = gr.Checkbox(label="Include a random meme/GIF?")
|
| 47 |
+
send_btn = gr.Button("Send")
|
| 48 |
+
|
| 49 |
+
with gr.Column():
|
| 50 |
+
chat_output = gr.HTML()
|
| 51 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
| 52 |
+
|
| 53 |
+
send_btn.click(fn=send_message,
|
| 54 |
+
inputs=[msg_input, include_meme],
|
| 55 |
+
outputs=[chat_output, status_output])\
|
| 56 |
+
.then(fn=format_chat, inputs=[chat_output], outputs=[chat_output])
|
| 57 |
+
|
| 58 |
+
demo.launch()
|