ddfws commited on
Commit
7aab66c
·
verified ·
1 Parent(s): 12afc42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -35
app.py CHANGED
@@ -1,36 +1,31 @@
 
 
1
  import gradio as gr
2
  import torch
3
 
4
  from transformers import (
5
  AutoTokenizer,
6
- AutoModelForCausalLM,
7
- BitsAndBytesConfig
8
  )
9
 
10
 
11
- model_id = "ddfws/Rezaeian-StatsAI"
12
 
13
 
14
  print("Loading tokenizer...")
15
 
16
- tokenizer = AutoTokenizer.from_pretrained(model_id)
 
 
17
 
18
 
19
  print("Loading model...")
20
 
21
 
22
- bnb_config = BitsAndBytesConfig(
23
- load_in_4bit=True,
24
- bnb_4bit_compute_dtype=torch.float16,
25
- bnb_4bit_quant_type="nf4",
26
- bnb_4bit_use_double_quant=True
27
- )
28
-
29
-
30
  model = AutoModelForCausalLM.from_pretrained(
31
- model_id,
32
- quantization_config=bnb_config,
33
- device_map="auto"
34
  )
35
 
36
 
@@ -41,20 +36,36 @@ def chat(message, history):
41
 
42
  messages = []
43
 
44
- for user, assistant in history:
45
- messages.append(
46
- {"role":"user","content":user}
47
- )
48
- messages.append(
49
- {"role":"assistant","content":assistant}
50
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  messages.append(
53
- {"role":"user","content":message}
 
 
 
54
  )
55
 
56
 
57
- text = tokenizer.apply_chat_template(
58
  messages,
59
  tokenize=False,
60
  add_generation_prompt=True
@@ -62,17 +73,24 @@ def chat(message, history):
62
 
63
 
64
  inputs = tokenizer(
65
- text,
66
  return_tensors="pt"
67
- ).to(model.device)
68
 
69
 
70
- output = model.generate(
71
- **inputs,
72
- max_new_tokens=512,
73
- temperature=0.7,
74
- do_sample=True
75
- )
 
 
 
 
 
 
 
76
 
77
 
78
  result = tokenizer.decode(
@@ -89,9 +107,14 @@ demo = gr.ChatInterface(
89
  fn=chat,
90
  title="Rezaeian-StatsAI",
91
  description="""
92
- فارسی AI آماری ساخته شده توسط امیرحسین رضائیان.
93
- Fine-tuned on university statistics notes.
94
- Base model: Qwen2.5-7B-Instruct.
 
 
 
 
 
95
  """
96
  )
97
 
 
1
+ import spaces
2
+
3
  import gradio as gr
4
  import torch
5
 
6
  from transformers import (
7
  AutoTokenizer,
8
+ AutoModelForCausalLM
 
9
  )
10
 
11
 
12
+ MODEL_ID = "ddfws/Rezaeian-StatsAI"
13
 
14
 
15
  print("Loading tokenizer...")
16
 
17
+ tokenizer = AutoTokenizer.from_pretrained(
18
+ MODEL_ID
19
+ )
20
 
21
 
22
  print("Loading model...")
23
 
24
 
 
 
 
 
 
 
 
 
25
  model = AutoModelForCausalLM.from_pretrained(
26
+ MODEL_ID,
27
+ device_map="auto",
28
+ torch_dtype=torch.float16
29
  )
30
 
31
 
 
36
 
37
  messages = []
38
 
39
+ for item in history:
40
+ if isinstance(item, dict):
41
+ messages.append(item)
42
+ else:
43
+ user_msg, bot_msg = item
44
+
45
+ messages.append(
46
+ {
47
+ "role": "user",
48
+ "content": user_msg
49
+ }
50
+ )
51
+
52
+ messages.append(
53
+ {
54
+ "role": "assistant",
55
+ "content": bot_msg
56
+ }
57
+ )
58
+
59
 
60
  messages.append(
61
+ {
62
+ "role": "user",
63
+ "content": message
64
+ }
65
  )
66
 
67
 
68
+ prompt = tokenizer.apply_chat_template(
69
  messages,
70
  tokenize=False,
71
  add_generation_prompt=True
 
73
 
74
 
75
  inputs = tokenizer(
76
+ prompt,
77
  return_tensors="pt"
78
+ )
79
 
80
 
81
+ if torch.cuda.is_available():
82
+ inputs = inputs.to("cuda")
83
+
84
+
85
+ with torch.no_grad():
86
+
87
+ output = model.generate(
88
+ **inputs,
89
+ max_new_tokens=512,
90
+ temperature=0.7,
91
+ top_p=0.9,
92
+ do_sample=True
93
+ )
94
 
95
 
96
  result = tokenizer.decode(
 
107
  fn=chat,
108
  title="Rezaeian-StatsAI",
109
  description="""
110
+ Rezaeian-StatsAI
111
+
112
+ Created by Amirhossein Rezaeian.
113
+
114
+ A Persian statistical AI model fine-tuned on University of Tehran statistics notes.
115
+
116
+ Base model:
117
+ Qwen2.5-7B-Instruct
118
  """
119
  )
120