FractalAIR commited on
Commit
7dbcac6
·
verified ·
1 Parent(s): 5e8c233

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -2
app.py CHANGED
@@ -6,6 +6,12 @@ from threading import Thread
6
  import re
7
  import uuid
8
 
 
 
 
 
 
 
9
  # Load model and tokenizer
10
  our_model_path = "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B"
11
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
@@ -24,8 +30,8 @@ conversations = {}
24
  def generate_conversation_id():
25
  return str(uuid.uuid4())[:8]
26
 
27
- @spaces.GPU(duration=60)
28
- def generate_response(user_message, max_tokens, temperature, top_p, history_state):
29
  if not user_message.strip():
30
  return history_state, history_state
31
 
@@ -85,8 +91,57 @@ def generate_response(user_message, max_tokens, temperature, top_p, history_stat
85
  pass
86
 
87
  yield new_history, new_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
 
90
  example_messages = {
91
  "JEE Main 2025 Combinatorics": "From all the English alphabets, five letters are chosen and are arranged in alphabetical order. The total number of ways, in which the middle letter is 'M', is?",
92
  "JEE Main 2025 Coordinate Geometry": "A circle \\(C\\) of radius 2 lies in the second quadrant and touches both the coordinate axes. Let \\(r\\) be the radius of a circle that has centre at the point \\((2, 5)\\) and intersects the circle \\(C\\) at exactly two points. If the set of all possible values of \\(r\\) is the interval \\((\\alpha, \\beta)\\), then \\(3\\beta - 2\\alpha\\) is?",
 
6
  import re
7
  import uuid
8
 
9
+ client = OpenAI(
10
+ base_url="https://a7g1ajqixo23revq.us-east-1.aws.endpoints.huggingface.cloud/v1/",
11
+ api_key="hf_XXXXX"
12
+ )
13
+
14
+
15
  # Load model and tokenizer
16
  our_model_path = "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B"
17
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
 
30
  def generate_conversation_id():
31
  return str(uuid.uuid4())[:8]
32
 
33
+ #@spaces.GPU(duration=60)
34
+ '''def generate_response(user_message, max_tokens, temperature, top_p, history_state):
35
  if not user_message.strip():
36
  return history_state, history_state
37
 
 
91
  pass
92
 
93
  yield new_history, new_history
94
+ '''
95
+
96
+ @spaces.GPU(duration=60)
97
+ def generate_response(user_message, max_tokens, temperature, top_p, history_state):
98
+ if not user_message.strip():
99
+ return history_state, history_state
100
+
101
+ start_tag = "<|im_start|>"
102
+ sep_tag = "<|im_sep|>"
103
+ end_tag = "<|im_end|>"
104
+
105
+ system_message = "Your role as an assistant..."
106
+ messages = [{"role": "system", "content": system_message}]
107
+ for message in history_state:
108
+ messages.append({"role": message["role"], "content": message["content"]})
109
+ messages.append({"role": "user", "content": user_message})
110
 
111
+ try:
112
+ response = client.chat.completions.create(
113
+ model="tgi",
114
+ messages=messages,
115
+ max_tokens=int(max_tokens),
116
+ temperature=temperature,
117
+ top_p=top_p,
118
+ stream=True,
119
+ )
120
+ except Exception as e:
121
+ yield history_state + [{"role": "user", "content": user_message},
122
+ {"role": "assistant", "content": "⚠️ Generation failed."}], history_state
123
+ return
124
+
125
+ assistant_response = ""
126
+ new_history = history_state + [
127
+ {"role": "user", "content": user_message},
128
+ {"role": "assistant", "content": ""}
129
+ ]
130
+
131
+ try:
132
+ for chunk in response:
133
+ if not chunk.choices or not chunk.choices[0].delta or not chunk.choices[0].delta.content:
134
+ continue
135
+ token = chunk.choices[0].delta.content
136
+ assistant_response += token
137
+ new_history[-1]["content"] = assistant_response.strip()
138
+ yield new_history, new_history
139
+ except Exception:
140
+ pass
141
+
142
+ yield new_history, new_history
143
 
144
+
145
  example_messages = {
146
  "JEE Main 2025 Combinatorics": "From all the English alphabets, five letters are chosen and are arranged in alphabetical order. The total number of ways, in which the middle letter is 'M', is?",
147
  "JEE Main 2025 Coordinate Geometry": "A circle \\(C\\) of radius 2 lies in the second quadrant and touches both the coordinate axes. Let \\(r\\) be the radius of a circle that has centre at the point \\((2, 5)\\) and intersects the circle \\(C\\) at exactly two points. If the set of all possible values of \\(r\\) is the interval \\((\\alpha, \\beta)\\), then \\(3\\beta - 2\\alpha\\) is?",