sayalimetkar commited on
Commit
caf786d
·
verified ·
1 Parent(s): 693aa4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -54
app.py CHANGED
@@ -3,13 +3,10 @@ import ast
3
  import operator
4
  import gradio as gr
5
  from ctransformers import AutoModelForCausalLM
6
-
7
  # ------------------------------
8
  # MODEL CONFIGURATION
9
  # ------------------------------
10
- # 👉 Use the correct path based on where you uploaded the model
11
  MODEL_PATH = "sayalimetkar/quant_model"
12
- # or "sayalimetkar/quant_model" if hosted on HF Hub
13
 
14
  SYSTEM_PROMPT = """
15
  You are a highly capable and reliable AI assistant.
@@ -19,8 +16,8 @@ Always give clear, direct, and correct answers without unnecessary explanation.
19
  If the user asks for code, return only properly formatted and working code.
20
  If the user asks for a calculation, show the reasoning and give the exact result.
21
  Always think step by step and explain the reasoning before giving the final answer.
22
- """
23
 
 
24
  FEW_SHOT_EXAMPLES = {
25
  "math": """... (unchanged) ...
26
  ### END OF EXAMPLES
@@ -41,10 +38,63 @@ def factorial(n):
41
  ### END OF EXAMPLES
42
  """
43
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  # ------------------------------
46
  # SAFE MATH SOLVER
47
  # ------------------------------
 
48
  operators = {
49
  ast.Add: operator.add,
50
  ast.Sub: operator.sub,
@@ -56,6 +106,7 @@ operators = {
56
  }
57
 
58
  def safe_eval(expr):
 
59
  def _eval(node):
60
  if isinstance(node, ast.Expression):
61
  return _eval(node.body)
@@ -81,64 +132,31 @@ def is_math_question(user_input):
81
  return bool(re.search(r'(\d+[\s\+\-\*/^()]|\bseries\b|\baverage\b|\bpercent|\bspeed|\btime|\bdistance\b)', user_input.lower()))
82
 
83
  def solve_math(user_input):
 
84
  try:
 
85
  expr = re.sub(r'[^0-9+\-*/().^%]', '', user_input)
86
  if not expr:
87
  return None
 
88
  expr = expr.replace('^', '**')
89
  result = safe_eval(expr)
90
  return str(result)
91
  except:
92
  return None
93
-
94
- # ------------------------------
95
- # FORMAT PROMPT
96
- # ------------------------------
97
- def format_prompt(system, history, user_input):
98
- if re.search(r'\b(def|SELECT|INSERT|UPDATE|print|for|while|if|class)\b', user_input, re.I):
99
- few_shot = FEW_SHOT_EXAMPLES["code"]
100
- task_type = "code"
101
- elif is_math_question(user_input):
102
- few_shot = FEW_SHOT_EXAMPLES["math"]
103
- task_type = "math"
104
- else:
105
- few_shot = ""
106
- task_type = "general"
107
-
108
- conversation = system.strip() + "\n\n" + few_shot.strip() + "\n\n"
109
- for user, assistant in history:
110
- conversation += f"User: {user}\nAssistant: {assistant}\n"
111
-
112
- if task_type == "code":
113
- tail = f"User: {user_input}\nAssistant: Provide ONLY the code block.\n### RESPONSE:\n"
114
- elif task_type == "math":
115
- tail = f"User: {user_input}\nAssistant: Solve step-by-step and give 'Final Answer:'.\n### RESPONSE:\n"
116
- else:
117
- tail = f"User: {user_input}\nAssistant: Provide a concise and direct answer.\n### RESPONSE:\n"
118
-
119
- conversation += tail
120
- return conversation
121
-
122
  # ------------------------------
123
  # LOAD MODEL
124
  # ------------------------------
125
- print("🚀 Loading model...")
126
-
127
- try:
128
- model = AutoModelForCausalLM.from_pretrained(
129
- "sayalimetkar/quant_model",
130
- model_type="mistral",
131
- temperature=0.2,
132
- top_p=0.9,
133
- top_k=50,
134
- repetition_penalty=1.1,
135
- context_length=4096,
136
- max_new_tokens=800
137
- )
138
- print("✅ Model loaded successfully!")
139
- except Exception as e:
140
- print(f"❌ Model failed to load: {e}")
141
-
142
  # ------------------------------
143
  # STREAM REPLY FUNCTION
144
  # ------------------------------
@@ -147,42 +165,51 @@ stop_flag = {"stop": False}
147
  def stream_reply(user_input, history):
148
  stop_flag["stop"] = False
149
 
 
150
  if is_math_question(user_input):
151
  math_answer = solve_math(user_input)
152
  if math_answer:
153
- yield history + [(user_input, math_answer)]
 
154
  return
155
 
 
156
  prompt = format_prompt(SYSTEM_PROMPT, history, user_input)
157
  partial = ""
158
  for token in model(prompt, stream=True):
159
  if stop_flag["stop"]:
160
  break
161
  partial += token
 
 
162
  cleaned = re.sub(r"(?i)(User:|Assistant:)", "", partial).strip()
 
163
  yield history + [(user_input, cleaned)]
164
 
165
  # ------------------------------
166
  # GRADIO UI
167
  # ------------------------------
168
  with gr.Blocks() as demo:
169
- gr.Markdown("## 🤖 Optimized Model Chatbot\nWelcome! Ask questions, request code, or solve math problems.")
170
  chatbot = gr.Chatbot(label="Chatbot")
171
  msg = gr.Textbox(label="Your message")
172
  send = gr.Button("Send")
173
  stop = gr.Button("🛑 Stop Response")
174
  reset = gr.Button("🔄 Reset Chat")
175
 
 
176
  def user_submit(user_message, history):
177
  return "", history + [(user_message, "")]
178
 
 
179
  def reset_chat():
180
  return []
181
 
 
182
  def stop_generation():
183
  stop_flag["stop"] = True
184
  return None
185
 
 
186
  msg.submit(user_submit, [msg, chatbot], [msg, chatbot]).then(
187
  stream_reply, [msg, chatbot], chatbot
188
  )
@@ -191,8 +218,7 @@ with gr.Blocks() as demo:
191
  )
192
  reset.click(reset_chat, outputs=chatbot)
193
  stop.click(stop_generation, None, None)
194
-
195
  # ------------------------------
196
- # LAUNCH APP (Hugging Face friendly)
197
  # ------------------------------
198
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
3
  import operator
4
  import gradio as gr
5
  from ctransformers import AutoModelForCausalLM
 
6
  # ------------------------------
7
  # MODEL CONFIGURATION
8
  # ------------------------------
 
9
  MODEL_PATH = "sayalimetkar/quant_model"
 
10
 
11
  SYSTEM_PROMPT = """
12
  You are a highly capable and reliable AI assistant.
 
16
  If the user asks for code, return only properly formatted and working code.
17
  If the user asks for a calculation, show the reasoning and give the exact result.
18
  Always think step by step and explain the reasoning before giving the final answer.
 
19
 
20
+ """
21
  FEW_SHOT_EXAMPLES = {
22
  "math": """... (unchanged) ...
23
  ### END OF EXAMPLES
 
38
  ### END OF EXAMPLES
39
  """
40
  }
41
+ # ------------------------------
42
+ # FORMAT PROMPT
43
+ # ------------------------------
44
+ def format_prompt(system: str, history: list[tuple[str, str]], user_input: str) -> str:
45
+ """
46
+ Format the full prompt including system message, few-shot examples, conversation history,
47
+ and a strict instruction to prevent extra/unrelated responses.
48
+ """
49
+
50
+ # --- Detect query type and choose few-shot examples ---
51
+ if re.search(r'\b(def|SELECT|INSERT|UPDATE|print|for|while|if|class)\b', user_input, re.I):
52
+ few_shot = FEW_SHOT_EXAMPLES["code"]
53
+ task_type = "code"
54
+ elif is_math_question(user_input):
55
+ few_shot = FEW_SHOT_EXAMPLES["math"]
56
+ task_type = "math"
57
+ else:
58
+ few_shot = ""
59
+ task_type = "general"
60
+
61
+ # --- Build base conversation ---
62
+ conversation = system.strip() + "\n\n" + few_shot.strip() + "\n\n"
63
+
64
+ # Add chat history
65
+ for user, assistant in history:
66
+ conversation += f"User: {user}\nAssistant: {assistant}\n"
67
+
68
+ # --- Add user input with explicit, single-task instruction ---
69
+ if task_type == "code":
70
+ tail = (
71
+ f"User: {user_input}\n"
72
+ "Assistant: Please provide ONLY the corrected or required code block. "
73
+ "Do NOT include explanations or any unrelated topics.\n### RESPONSE:\n"
74
+ )
75
+
76
+ elif task_type == "math":
77
+ tail = (
78
+ f"User: {user_input}\n"
79
+ "Assistant: Let's think step by step. Then provide ONLY the final numeric answer, "
80
+ "on a new line prefixed by 'Final Answer:'.\n### RESPONSE:\n"
81
+ )
82
+
83
+ else: # general queries
84
+ tail = (
85
+ f"User: {user_input}\n"
86
+ "Assistant: Provide a concise and direct answer. "
87
+ "Do NOT add examples, explanations, or unrelated information.\n### RESPONSE:\n"
88
+ )
89
+
90
+ conversation += tail
91
+ return conversation
92
+
93
 
94
  # ------------------------------
95
  # SAFE MATH SOLVER
96
  # ------------------------------
97
+ # Supported operators
98
  operators = {
99
  ast.Add: operator.add,
100
  ast.Sub: operator.sub,
 
106
  }
107
 
108
  def safe_eval(expr):
109
+ """Safely evaluate arithmetic expressions using AST."""
110
  def _eval(node):
111
  if isinstance(node, ast.Expression):
112
  return _eval(node.body)
 
132
  return bool(re.search(r'(\d+[\s\+\-\*/^()]|\bseries\b|\baverage\b|\bpercent|\bspeed|\btime|\bdistance\b)', user_input.lower()))
133
 
134
  def solve_math(user_input):
135
+ """Solve any arithmetic expression safely."""
136
  try:
137
+ # Keep only numbers, operators, parentheses
138
  expr = re.sub(r'[^0-9+\-*/().^%]', '', user_input)
139
  if not expr:
140
  return None
141
+ # Replace ^ with ** for exponentiation
142
  expr = expr.replace('^', '**')
143
  result = safe_eval(expr)
144
  return str(result)
145
  except:
146
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  # ------------------------------
148
  # LOAD MODEL
149
  # ------------------------------
150
+ model = AutoModelForCausalLM.from_pretrained(
151
+ "sayalimetkar/quant_model",
152
+ model_type="mistral",
153
+ temperature=0.2,
154
+ top_p=0.9,
155
+ top_k=50,
156
+ repetition_penalty=1.1,
157
+ context_length=4096,
158
+ max_new_tokens=800
159
+ )
 
 
 
 
 
 
 
160
  # ------------------------------
161
  # STREAM REPLY FUNCTION
162
  # ------------------------------
 
165
  def stream_reply(user_input, history):
166
  stop_flag["stop"] = False
167
 
168
+ # 1️⃣ Handle direct arithmetic
169
  if is_math_question(user_input):
170
  math_answer = solve_math(user_input)
171
  if math_answer:
172
+ cleaned = re.sub(r"(?i)(User:|Assistant:)", "", partial).strip()
173
+ yield history + [(user_input, cleaned)]
174
  return
175
 
176
+ # 2️⃣ Let model handle reasoning or coding
177
  prompt = format_prompt(SYSTEM_PROMPT, history, user_input)
178
  partial = ""
179
  for token in model(prompt, stream=True):
180
  if stop_flag["stop"]:
181
  break
182
  partial += token
183
+
184
+ # Clean prefixes
185
  cleaned = re.sub(r"(?i)(User:|Assistant:)", "", partial).strip()
186
+
187
  yield history + [(user_input, cleaned)]
188
 
189
  # ------------------------------
190
  # GRADIO UI
191
  # ------------------------------
192
  with gr.Blocks() as demo:
 
193
  chatbot = gr.Chatbot(label="Chatbot")
194
  msg = gr.Textbox(label="Your message")
195
  send = gr.Button("Send")
196
  stop = gr.Button("🛑 Stop Response")
197
  reset = gr.Button("🔄 Reset Chat")
198
 
199
+ # Add message to history
200
  def user_submit(user_message, history):
201
  return "", history + [(user_message, "")]
202
 
203
+ # Reset chat
204
  def reset_chat():
205
  return []
206
 
207
+ # Stop current generation
208
  def stop_generation():
209
  stop_flag["stop"] = True
210
  return None
211
 
212
+ # UI Event Handlers
213
  msg.submit(user_submit, [msg, chatbot], [msg, chatbot]).then(
214
  stream_reply, [msg, chatbot], chatbot
215
  )
 
218
  )
219
  reset.click(reset_chat, outputs=chatbot)
220
  stop.click(stop_generation, None, None)
 
221
  # ------------------------------
222
+ # LAUNCH APP
223
  # ------------------------------
224
  demo.launch(server_name="0.0.0.0", server_port=7860)