arad1367 commited on
Commit
28fc680
·
verified ·
1 Parent(s): fb74963

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -17
app.py CHANGED
@@ -3,7 +3,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
  import gradio as gr
5
 
6
- # Model identifier
7
  model_name = "Qwen/Qwen2.5-3B-Instruct"
8
 
9
  # Load tokenizer and model
@@ -18,21 +18,16 @@ model = AutoModelForCausalLM.from_pretrained(
18
  trust_remote_code=True,
19
  )
20
 
21
- # Chat function (no history used for simplicity and compatibility)
22
  def respond(message, history):
23
  messages = [{"role": "user", "content": message}]
24
-
25
- # Apply chat template
26
  prompt = tokenizer.apply_chat_template(
27
  messages,
28
  tokenize=False,
29
  add_generation_prompt=True
30
  )
31
-
32
- # Tokenize
33
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
34
 
35
- # Generate
36
  with torch.no_grad():
37
  outputs = model.generate(
38
  **inputs,
@@ -42,24 +37,23 @@ def respond(message, history):
42
  do_sample=True,
43
  pad_token_id=tokenizer.eos_token_id,
44
  )
45
-
46
- # Decode response
47
  response = tokenizer.decode(
48
- outputs[0][inputs['input_ids'].shape[-1]:],
49
  skip_special_tokens=True
50
  )
51
  return response
52
 
53
- # Gradio Interface — NO retry_btn / undo_btn (to avoid version issues)
 
54
  demo = gr.ChatInterface(
55
  fn=respond,
56
- title="Qwen2.5-3B-Instruct Chatbot",
57
- description="Ask me anything! I'm a 3B AI assistant by Alibaba Cloud.",
58
  examples=[
59
- "Explain quantum computing in simple terms.",
60
- "Write a Python function to check if a number is prime.",
61
- "Solve: 3x + 5 = 17"
62
- ],
63
  )
64
 
65
  # Launch
 
3
  import torch
4
  import gradio as gr
5
 
6
+ # Model name
7
  model_name = "Qwen/Qwen2.5-3B-Instruct"
8
 
9
  # Load tokenizer and model
 
18
  trust_remote_code=True,
19
  )
20
 
21
+ # Chat function
22
  def respond(message, history):
23
  messages = [{"role": "user", "content": message}]
 
 
24
  prompt = tokenizer.apply_chat_template(
25
  messages,
26
  tokenize=False,
27
  add_generation_prompt=True
28
  )
 
 
29
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
30
 
 
31
  with torch.no_grad():
32
  outputs = model.generate(
33
  **inputs,
 
37
  do_sample=True,
38
  pad_token_id=tokenizer.eos_token_id,
39
  )
 
 
40
  response = tokenizer.decode(
41
+ outputs[0][inputs["input_ids"].shape[-1]:],
42
  skip_special_tokens=True
43
  )
44
  return response
45
 
46
+ # Create Gradio ChatInterface
47
+ # Gradio 3.50.2 supports ChatInterface fully
48
  demo = gr.ChatInterface(
49
  fn=respond,
50
+ title="Qwen2.5-3B Chatbot",
51
+ description="Ask me anything! I'm a smart AI assistant by Alibaba Cloud.",
52
  examples=[
53
+ "Explain relativity in simple terms.",
54
+ "Write a Python function to reverse a string.",
55
+ "Solve: 2x + 8 = 20"
56
+ ]
57
  )
58
 
59
  # Launch