Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,15 +12,16 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 12 |
device_map="auto"
|
| 13 |
)
|
| 14 |
|
| 15 |
-
# Streaming response
|
| 16 |
def chat_with_dudea(message, history):
|
| 17 |
-
messages = [{"role": "system", "content": "You are DUDEAIBeta1.1, a
|
| 18 |
for user_msg, bot_msg in history:
|
| 19 |
messages.append({"role": "user", "content": user_msg})
|
| 20 |
if bot_msg:
|
| 21 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 22 |
messages.append({"role": "user", "content": message})
|
| 23 |
|
|
|
|
| 24 |
inputs = tokenizer.apply_chat_template(
|
| 25 |
messages,
|
| 26 |
add_generation_prompt=True,
|
|
@@ -29,48 +30,53 @@ def chat_with_dudea(message, history):
|
|
| 29 |
return_tensors="pt"
|
| 30 |
).to(model.device)
|
| 31 |
|
|
|
|
| 32 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 33 |
-
generation_kwargs = dict(inputs, max_new_tokens=
|
|
|
|
|
|
|
| 34 |
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
| 35 |
thread.start()
|
| 36 |
|
|
|
|
| 37 |
partial_response = ""
|
| 38 |
for new_text in streamer:
|
| 39 |
partial_response += new_text
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
# Custom UI
|
| 43 |
with gr.Blocks(css="""
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
.user {flex-direction: row-reverse;}
|
| 47 |
-
.avatar {width:
|
| 48 |
-
.bubble {padding:
|
| 49 |
-
.user .bubble {background
|
| 50 |
-
.bot .bubble {background
|
|
|
|
| 51 |
""") as demo:
|
| 52 |
gr.Markdown(
|
| 53 |
-
"<h1 style='text-align: center;'>🤖 DUDEAIBeta1.1</h1>"
|
| 54 |
-
"<p style='text-align:center;'>
|
| 55 |
)
|
| 56 |
|
| 57 |
-
# 👇 put your avatar files in the Space repo (user.png, ai.png)
|
| 58 |
chatbot = gr.Chatbot(
|
| 59 |
elem_id="chatbox",
|
| 60 |
-
avatar_images=("user.png", "ai.png") #
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
|
|
|
| 66 |
def respond(message, history):
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
for partial in response_stream:
|
| 70 |
-
history[-1] = (message, partial)
|
| 71 |
-
yield "", history
|
| 72 |
|
| 73 |
-
msg.submit(respond, [msg, chatbot], [
|
| 74 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 75 |
|
| 76 |
demo.launch()
|
|
|
|
| 12 |
device_map="auto"
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# Streaming response generator
|
| 16 |
def chat_with_dudea(message, history):
|
| 17 |
+
messages = [{"role": "system", "content": "You are DUDEAIBeta1.1, a futuristic, smooth-talking assistant."}]
|
| 18 |
for user_msg, bot_msg in history:
|
| 19 |
messages.append({"role": "user", "content": user_msg})
|
| 20 |
if bot_msg:
|
| 21 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 22 |
messages.append({"role": "user", "content": message})
|
| 23 |
|
| 24 |
+
# Tokenize
|
| 25 |
inputs = tokenizer.apply_chat_template(
|
| 26 |
messages,
|
| 27 |
add_generation_prompt=True,
|
|
|
|
| 30 |
return_tensors="pt"
|
| 31 |
).to(model.device)
|
| 32 |
|
| 33 |
+
# Create streamer
|
| 34 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 35 |
+
generation_kwargs = dict(inputs, max_new_tokens=300, streamer=streamer)
|
| 36 |
+
|
| 37 |
+
# Run model.generate in a background thread
|
| 38 |
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
| 39 |
thread.start()
|
| 40 |
|
| 41 |
+
# Build response incrementally
|
| 42 |
partial_response = ""
|
| 43 |
for new_text in streamer:
|
| 44 |
partial_response += new_text
|
| 45 |
+
history[-1] = (message, partial_response) # update last message
|
| 46 |
+
yield history # 🔥 this yields live typing updates
|
| 47 |
|
| 48 |
# Custom UI
|
| 49 |
with gr.Blocks(css="""
|
| 50 |
+
body {background: linear-gradient(135deg, #1e1e2f, #2c2c54); color: white; font-family: 'Inter', sans-serif;}
|
| 51 |
+
#chatbox {height: 600px; overflow-y: auto; background: rgba(255,255,255,0.05); border-radius: 20px; padding: 20px;}
|
| 52 |
+
.message {display: flex; align-items: flex-start; margin: 12px;}
|
| 53 |
.user {flex-direction: row-reverse;}
|
| 54 |
+
.avatar {width: 45px; height: 45px; border-radius: 50%; margin: 8px;}
|
| 55 |
+
.bubble {padding: 14px 18px; border-radius: 18px; max-width: 70%; animation: fadeIn 0.3s ease;}
|
| 56 |
+
.user .bubble {background: linear-gradient(135deg, #4f46e5, #6366f1); color: white; border-bottom-right-radius: 6px;}
|
| 57 |
+
.bot .bubble {background: rgba(255,255,255,0.1); color: #f3f4f6; border-bottom-left-radius: 6px; backdrop-filter: blur(8px);}
|
| 58 |
+
@keyframes fadeIn {from {opacity:0; transform: translateY(10px);} to {opacity:1; transform: translateY(0);}}
|
| 59 |
""") as demo:
|
| 60 |
gr.Markdown(
|
| 61 |
+
"<h1 style='text-align: center; color:#9f9fff;'>🤖 DUDEAIBeta1.1</h1>"
|
| 62 |
+
"<p style='text-align:center; opacity:0.8;'>A smooth, next-gen AI chat with <b>live typing</b> ✨</p>"
|
| 63 |
)
|
| 64 |
|
|
|
|
| 65 |
chatbot = gr.Chatbot(
|
| 66 |
elem_id="chatbox",
|
| 67 |
+
avatar_images=("user.png", "ai.png") # put these files in your repo
|
| 68 |
)
|
| 69 |
|
| 70 |
+
with gr.Row():
|
| 71 |
+
msg = gr.Textbox(placeholder="Type your message...", container=False, scale=10)
|
| 72 |
+
clear = gr.Button("🧹", scale=1)
|
| 73 |
|
| 74 |
+
# Live streaming respond
|
| 75 |
def respond(message, history):
|
| 76 |
+
history.append((message, "")) # add user message
|
| 77 |
+
return chat_with_dudea(message, history)
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
msg.submit(respond, [msg, chatbot], [chatbot])
|
| 80 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 81 |
|
| 82 |
demo.launch()
|