Emalawi19 commited on
Commit
312f78d
·
verified ·
1 Parent(s): ac75fba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -17
app.py CHANGED
@@ -6,6 +6,7 @@ model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
6
  print("Loading model...")
7
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
9
  model = AutoModelForCausalLM.from_pretrained(
10
  model_name,
11
  device_map="auto"
@@ -14,33 +15,45 @@ model = AutoModelForCausalLM.from_pretrained(
14
  print("Model loaded!")
15
 
16
  def chat(message, history):
17
- conversation = ""
 
 
 
 
 
 
 
18
 
19
- for user, bot in history:
20
- conversation += f"User: {user}\nAssistant: {bot}\n"
 
 
 
 
21
 
22
- conversation += f"User: {message}\nAssistant:"
 
 
 
 
 
 
 
23
 
24
- inputs = tokenizer(conversation, return_tensors="pt").to(model.device)
25
 
26
- outputs = model.generate(
27
- **inputs,
28
- max_new_tokens=300,
29
- temperature=0.8,
30
- top_p=0.95,
31
- repetition_penalty=1.1,
32
- do_sample=True
33
- )
34
 
35
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
36
- response = response.split("Assistant:")[-1].strip()
37
 
38
- return response
 
39
 
40
  iface = gr.ChatInterface(
41
  fn=chat,
42
  title="Emalawi19 AI 🤖",
43
- description="Your own AI assistant powered by DeepSeek"
44
  )
45
 
46
  iface.launch()
 
6
  print("Loading model...")
7
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+
10
  model = AutoModelForCausalLM.from_pretrained(
11
  model_name,
12
  device_map="auto"
 
15
  print("Model loaded!")
16
 
17
  def chat(message, history):
18
+ try:
19
+ conversation = ""
20
+
21
+ # Limit history to avoid overload
22
+ for user, bot in history[-3:]:
23
+ conversation += f"User: {user}\nAssistant: {bot}\n"
24
+
25
+ conversation += f"User: {message}\nAssistant:"
26
 
27
+ inputs = tokenizer(
28
+ conversation,
29
+ return_tensors="pt",
30
+ truncation=True,
31
+ max_length=1024
32
+ ).to(model.device)
33
 
34
+ outputs = model.generate(
35
+ **inputs,
36
+ max_new_tokens=150,
37
+ temperature=0.7,
38
+ top_p=0.9,
39
+ repetition_penalty=1.1,
40
+ do_sample=True
41
+ )
42
 
43
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
44
 
45
+ # Extract only latest response
46
+ response = response.split("Assistant:")[-1].strip()
 
 
 
 
 
 
47
 
48
+ return response
 
49
 
50
+ except Exception as e:
51
+ return "⚠️ Error: Model overloaded. Try again with a shorter question."
52
 
53
  iface = gr.ChatInterface(
54
  fn=chat,
55
  title="Emalawi19 AI 🤖",
56
+ description="Chat with your own AI assistant powered by DeepSeek"
57
  )
58
 
59
  iface.launch()