jorker121 commited on
Commit
903a631
Β·
1 Parent(s): 64bc501

Deploy DeepSeek LLM chatbot

Browse files
Files changed (2) hide show
  1. app.py +66 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # Load DeepSeek LLM
6
+ model_name = "deepseek-ai/deepseek-llm-7b-chat"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
9
+
10
+ SYSTEM_PROMPT = "You are a helpful AI assistant. Keep responses concise and informative."
11
+
12
+ def generate_response(message, history):
13
+ history = history or []
14
+ history.append(("User", message))
15
+
16
+ # Add system message for better guidance
17
+ input_text = f"[SYSTEM] {SYSTEM_PROMPT}\n" + tokenizer.apply_chat_template(history, tokenize=False, add_generation_prompt=True)
18
+ inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
19
+
20
+ streamer = tokenizer.streamer()
21
+ model.generate(**inputs, streamer=streamer, max_length=512, pad_token_id=tokenizer.eos_token_id)
22
+
23
+ bot_message = ""
24
+ for token in streamer:
25
+ bot_message += token
26
+ yield bot_message
27
+
28
+ # Create Gradio Chatbot UI with streaming
29
+ with gr.Blocks() as demo:
30
+ gr.Markdown("### πŸš€ DeepSeek LLM Chatbot (Streaming & Improved UI)")
31
+
32
+ chatbot = gr.Chatbot()
33
+ msg = gr.Textbox(placeholder="Type your message here...", label="Your Message")
34
+ clear_btn = gr.Button("Clear Chat")
35
+
36
+ def respond(message, history):
37
+ history = history or []
38
+ bot_response = generate_response(message, history)
39
+ return bot_response, history + [("User", message), ("Bot", bot_response)]
40
+
41
+ msg.submit(respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
42
+ clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
43
+
44
+ demo.launch()
45
+
46
+
47
+
48
+ # with gr.Blocks() as demo:
49
+ # gr.Markdown("### πŸš€ DeepSeek LLM Chatbot (Streaming Enabled)")
50
+ # chat = gr.ChatInterface(fn=generate_response)
51
+
52
+ # demo.launch()
53
+
54
+ '''
55
+ βœ… Uses streaming (streamer=tokenizer.streamer())
56
+ βœ… Returns tokens in real-time instead of waiting for full response
57
+ βœ… Improved UI with gr.Blocks()
58
+
59
+ βœ… System prompt ensures responses are concise & helpful
60
+ βœ… Chat history is structured more clearly
61
+
62
+ βœ… Retains chat history
63
+ βœ… "Clear Chat" button
64
+ βœ… Better UI layout with Markdown & structured input boxes
65
+
66
+ '''
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+ accelerate