Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,16 +15,51 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
15 |
# Create pipeline
|
16 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Gradio UI
|
24 |
-
gr.
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
15 |
# Create pipeline
|
16 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
17 |
|
18 |
+
# Memory to store chat history
|
19 |
+
chat_history = []
|
20 |
+
|
21 |
+
# Format prompt with history
|
22 |
+
def format_prompt(history, user_input):
|
23 |
+
prompt = ""
|
24 |
+
for i, (user, bot) in enumerate(history):
|
25 |
+
prompt += f"User: {user}\nAssistant: {bot}\n"
|
26 |
+
prompt += f"User: {user_input}\nAssistant:"
|
27 |
+
return prompt
|
28 |
+
|
29 |
+
# Chat function
|
30 |
+
def chat(user_input):
|
31 |
+
global chat_history
|
32 |
+
prompt = format_prompt(chat_history, user_input)
|
33 |
+
output = pipe(prompt, max_new_tokens=200, do_sample=True, temperature=0.7)[0]['generated_text']
|
34 |
+
|
35 |
+
# Extract new response only (everything after last "Assistant:")
|
36 |
+
assistant_response = output.split("Assistant:")[-1].strip()
|
37 |
+
|
38 |
+
# Add to history
|
39 |
+
chat_history.append((user_input, assistant_response))
|
40 |
+
|
41 |
+
# Build full conversation display
|
42 |
+
chat_display = ""
|
43 |
+
for user, bot in chat_history:
|
44 |
+
chat_display += f"🧑💻 User: {user}\n🤖 Assistant: {bot}\n\n"
|
45 |
+
|
46 |
+
return chat_display.strip()
|
47 |
+
|
48 |
+
# Reset chat
|
49 |
+
def reset_chat():
|
50 |
+
global chat_history
|
51 |
+
chat_history = []
|
52 |
+
return ""
|
53 |
|
54 |
# Gradio UI
|
55 |
+
with gr.Blocks(title="🧠 DeepSeek 7B Chat with Memory") as demo:
|
56 |
+
gr.Markdown("## 🤖 DeepSeek Coder R1 7B\nChat with memory. Ask coding questions or continue a conversation.")
|
57 |
+
chatbot = gr.Textbox(lines=20, interactive=False, label="Chat History")
|
58 |
+
msg = gr.Textbox(label="Type your message here", placeholder="What can I help you with today?")
|
59 |
+
send_btn = gr.Button("Send")
|
60 |
+
clear_btn = gr.Button("🧹 Clear Chat")
|
61 |
+
|
62 |
+
send_btn.click(chat, inputs=msg, outputs=chatbot)
|
63 |
+
clear_btn.click(reset_chat, outputs=chatbot)
|
64 |
+
|
65 |
+
demo.launch()
|