Emalawi19 commited on
Commit
01ea371
·
verified ·
1 Parent(s): c838a56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -15
app.py CHANGED
@@ -2,31 +2,23 @@ import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
- # Use the smaller 1.5B model for stability
6
  model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
7
 
8
  print("Loading model...")
9
-
10
  tokenizer = AutoTokenizer.from_pretrained(model_name)
11
-
12
  model = AutoModelForCausalLM.from_pretrained(
13
  model_name,
14
  device_map="auto"
15
  )
16
-
17
  print("Model loaded!")
18
 
19
  def chat(message, history):
20
  try:
21
  conversation = ""
22
-
23
- # Keep last 3 messages to avoid overload
24
  for user, bot in history[-3:]:
25
  conversation += f"User: {user}\nAssistant: {bot}\n"
26
-
27
  conversation += f"User: {message}\nAssistant:"
28
 
29
- # Tokenize with truncation
30
  inputs = tokenizer(
31
  conversation,
32
  return_tensors="pt",
@@ -44,20 +36,16 @@ def chat(message, history):
44
  )
45
 
46
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
47
-
48
- # Keep only latest assistant response
49
  response = response.split("Assistant:")[-1].strip()
50
-
51
  return response
52
 
53
  except Exception as e:
54
- return f"⚠️ Error: Model overloaded. Try again with a shorter question."
55
 
56
- # Gradio chat interface
57
  iface = gr.ChatInterface(
58
  fn=chat,
59
- title="Emalawi19 AI 🤖",
60
- description="Chat with your own AI assistant powered by DeepSeek"
61
  )
62
 
63
  iface.launch()
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
 
5
  model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
6
 
7
  print("Loading model...")
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
9
  model = AutoModelForCausalLM.from_pretrained(
10
  model_name,
11
  device_map="auto"
12
  )
 
13
  print("Model loaded!")
14
 
15
  def chat(message, history):
16
  try:
17
  conversation = ""
 
 
18
  for user, bot in history[-3:]:
19
  conversation += f"User: {user}\nAssistant: {bot}\n"
 
20
  conversation += f"User: {message}\nAssistant:"
21
 
 
22
  inputs = tokenizer(
23
  conversation,
24
  return_tensors="pt",
 
36
  )
37
 
38
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
39
  response = response.split("Assistant:")[-1].strip()
 
40
  return response
41
 
42
  except Exception as e:
43
+ return f"Error: {str(e)}"
44
 
 
45
  iface = gr.ChatInterface(
46
  fn=chat,
47
+ title="DeepSeek Chat AI",
48
+ description="Chat with DeepSeek 1.5B model"
49
  )
50
 
51
  iface.launch()