Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,126 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
3 |
|
4 |
-
# ---------
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
headers = {
|
16 |
-
"Authorization": f"Bearer {HF_TOKEN}",
|
17 |
-
"Content-Type": "application/json"
|
18 |
-
}
|
19 |
-
payload = {"inputs": message}
|
20 |
-
try:
|
21 |
-
response = requests.post(HF_API_URL, headers=headers, json=payload)
|
22 |
-
data = response.json()
|
23 |
-
reply = "⚠️ No response from AI"
|
24 |
-
if isinstance(data, list) and len(data) > 0:
|
25 |
-
if "generated_text" in data[0]:
|
26 |
-
reply = data[0]["generated_text"]
|
27 |
-
elif "generated_texts" in data[0] and len(data[0]["generated_texts"]) > 0:
|
28 |
-
reply = data[0]["generated_texts"][0]
|
29 |
-
elif isinstance(data[0], str):
|
30 |
-
reply = data[0]
|
31 |
-
elif isinstance(data, str):
|
32 |
-
reply = data
|
33 |
-
return reply
|
34 |
-
except Exception as e:
|
35 |
-
return f"⚠️ Error: {e}"
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
<head>
|
42 |
-
<meta name="google-site-verification" content="
|
43 |
</head>
|
44 |
""")
|
45 |
|
46 |
-
gr.Markdown("## 💬 Mem-Agent AI Chat")
|
47 |
-
|
48 |
with gr.Column():
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
with gr.Row():
|
53 |
-
|
54 |
-
placeholder="Type your message
|
55 |
-
|
|
|
56 |
)
|
57 |
-
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
chat.append({"role": "user", "content": f"{message}"})
|
63 |
-
chat.append({"role": "assistant", "content": f"{ai_reply}"})
|
64 |
-
return "", chat
|
65 |
|
66 |
-
|
67 |
-
|
|
|
68 |
|
69 |
-
# --------- Launch ---------
|
70 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
|
5 |
+
# --------- Load model ---------
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("driaforall/mem-agent", trust_remote_code=True)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained("driaforall/mem-agent", trust_remote_code=True)
|
8 |
+
model.eval()
|
9 |
|
10 |
+
# --------- Chat function ---------
|
11 |
+
def chat_fn(history, message):
|
12 |
+
if not message.strip():
|
13 |
+
return history, gr.update(value="") # ignore empty messages
|
14 |
|
15 |
+
messages = [{"role": "user", "content": message}]
|
16 |
+
inputs = tokenizer.apply_chat_template(
|
17 |
+
messages,
|
18 |
+
add_generation_prompt=True,
|
19 |
+
tokenize=True,
|
20 |
+
return_dict=True,
|
21 |
+
return_tensors="pt"
|
22 |
+
).to(model.device)
|
23 |
|
24 |
+
outputs = model.generate(**inputs, max_new_tokens=60, do_sample=True, temperature=0.8)
|
25 |
+
reply = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
history.append((message, reply))
|
28 |
+
return history, gr.update(value="") # reset input box
|
29 |
+
|
30 |
+
# --------- CSS for chat bubbles ---------
|
31 |
+
css = """
|
32 |
+
body {
|
33 |
+
background: linear-gradient(135deg, #141e30, #243b55);
|
34 |
+
font-family: 'Poppins', sans-serif;
|
35 |
+
color: white;
|
36 |
+
}
|
37 |
+
|
38 |
+
#chatbox {
|
39 |
+
border-radius: 20px;
|
40 |
+
padding: 10px;
|
41 |
+
background: rgba(0,0,0,0.3);
|
42 |
+
box-shadow: 0 0 20px rgba(0,0,0,0.5);
|
43 |
+
max-height: 500px;
|
44 |
+
overflow-y: auto;
|
45 |
+
}
|
46 |
+
|
47 |
+
.user-msg {
|
48 |
+
display: flex;
|
49 |
+
justify-content: flex-end;
|
50 |
+
margin: 8px 0;
|
51 |
+
}
|
52 |
+
.bot-msg {
|
53 |
+
display: flex;
|
54 |
+
justify-content: flex-start;
|
55 |
+
margin: 8px 0;
|
56 |
+
}
|
57 |
+
.avatar {
|
58 |
+
width: 40px;
|
59 |
+
height: 40px;
|
60 |
+
border-radius: 50%;
|
61 |
+
margin: 0 8px;
|
62 |
+
}
|
63 |
+
.user-bubble {
|
64 |
+
background: #2f88ff;
|
65 |
+
color: white;
|
66 |
+
border-radius: 18px 18px 4px 18px;
|
67 |
+
padding: 10px 14px;
|
68 |
+
max-width: 60%;
|
69 |
+
}
|
70 |
+
.bot-bubble {
|
71 |
+
background: #3a3a3a;
|
72 |
+
color: #f5f5f5;
|
73 |
+
border-radius: 18px 18px 18px 4px;
|
74 |
+
padding: 10px 14px;
|
75 |
+
max-width: 60%;
|
76 |
+
}
|
77 |
+
"""
|
78 |
+
|
79 |
+
# --------- Render chat history ---------
|
80 |
+
def render_chat(history):
|
81 |
+
chat_html = ""
|
82 |
+
for user_msg, bot_msg in history:
|
83 |
+
chat_html += f"""
|
84 |
+
<div class="user-msg">
|
85 |
+
<div class="user-bubble">{user_msg}</div>
|
86 |
+
<img class="avatar" src="https://i.imgur.com/4M34hi2.png" alt="User">
|
87 |
+
</div>
|
88 |
+
<div class="bot-msg">
|
89 |
+
<img class="avatar" src="https://i.imgur.com/8Km9tLL.png" alt="AI">
|
90 |
+
<div class="bot-bubble">{bot_msg}</div>
|
91 |
+
</div>
|
92 |
+
"""
|
93 |
+
return chat_html
|
94 |
+
|
95 |
+
# --------- Gradio UI ---------
|
96 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
97 |
+
# Inject Google Site Verification meta tag
|
98 |
+
gr.HTML("""
|
99 |
<head>
|
100 |
+
<meta name="google-site-verification" content="OXT03M7HRqpImYW2myqTjRmEP_f8wZi2SnbrO0JmGcc" />
|
101 |
</head>
|
102 |
""")
|
103 |
|
|
|
|
|
104 |
with gr.Column():
|
105 |
+
gr.HTML("<h1 style='text-align:center; color:#00fff7;'>💬 AI Chat (Discord Style)</h1>")
|
106 |
+
chatbox = gr.HTML("", elem_id="chatbox")
|
|
|
107 |
with gr.Row():
|
108 |
+
msg = gr.Textbox(
|
109 |
+
placeholder="Type your message...",
|
110 |
+
show_label=False,
|
111 |
+
container=False,
|
112 |
)
|
113 |
+
send = gr.Button("Send 🚀")
|
114 |
+
clear = gr.Button("Clear Chat 🧹")
|
115 |
+
|
116 |
+
state = gr.State([])
|
117 |
|
118 |
+
def process_message(history, message):
|
119 |
+
history, _ = chat_fn(history, message)
|
120 |
+
return history, render_chat(history), ""
|
|
|
|
|
|
|
121 |
|
122 |
+
send.click(process_message, [state, msg], [state, chatbox, msg])
|
123 |
+
msg.submit(process_message, [state, msg], [state, chatbox, msg])
|
124 |
+
clear.click(lambda: ([], "", ""), None, [state, chatbox, msg])
|
125 |
|
|
|
126 |
demo.launch()
|