Hrushi02 commited on
Commit
e02d7d6
ยท
verified ยท
1 Parent(s): 7ee65b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -8
app.py CHANGED
@@ -5,7 +5,8 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
5
  from peft import PeftModel
6
 
7
  """
8
- Root_Math fine-tuned model chat app for Hugging Face Spaces.
 
9
  """
10
 
11
  # โœ… Load Hugging Face API token securely
@@ -15,7 +16,7 @@ if not api_token:
15
 
16
  # โœ… Define model names
17
  base_model_name = "unsloth/qwen2.5-math-7b-bnb-4bit"
18
- peft_model_name = "Hrushi02/Root_Math" # <-- stays the same
19
 
20
  # โœ… Load base model
21
  print("๐Ÿ”„ Loading base model...")
@@ -34,7 +35,8 @@ model = PeftModel.from_pretrained(base_model, peft_model_name, token=api_token)
34
  print("๐Ÿ”„ Loading tokenizer...")
35
  tokenizer = AutoTokenizer.from_pretrained(base_model_name, token=api_token)
36
 
37
- # โœ… Define chat response function
 
38
  def respond(message, history, system_message, max_tokens, temperature, top_p):
39
  """Generate responses from your fine-tuned model."""
40
  full_prompt = system_message + "\n\n"
@@ -57,16 +59,19 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
57
  )
58
 
59
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
60
- # extract only the assistant's last message
 
61
  if "Assistant:" in response:
62
  response = response.split("Assistant:")[-1].strip()
63
 
64
- yield response
65
 
66
 
67
- # โœ… Create Gradio interface
68
- demo = gr.ChatInterface(
69
- respond,
 
 
70
  additional_inputs=[
71
  gr.Textbox(value="You are a helpful math assistant.", label="System message"),
72
  gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="Max new tokens"),
@@ -78,6 +83,26 @@ demo = gr.ChatInterface(
78
  )
79
 
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  # โœ… Launch app
82
  if __name__ == "__main__":
83
  demo.launch()
 
5
  from peft import PeftModel
6
 
7
  """
8
+ ๐Ÿงฎ Root_Math fine-tuned model chat app for Hugging Face Spaces.
9
+ Supports both Gradio UI and API access via `/chat`.
10
  """
11
 
12
  # โœ… Load Hugging Face API token securely
 
16
 
17
  # โœ… Define model names
18
  base_model_name = "unsloth/qwen2.5-math-7b-bnb-4bit"
19
+ peft_model_name = "Hrushi02/Root_Math" # <-- model name stays the same
20
 
21
  # โœ… Load base model
22
  print("๐Ÿ”„ Loading base model...")
 
35
  print("๐Ÿ”„ Loading tokenizer...")
36
  tokenizer = AutoTokenizer.from_pretrained(base_model_name, token=api_token)
37
 
38
+
39
+ # โœ… Define the response function
40
  def respond(message, history, system_message, max_tokens, temperature, top_p):
41
  """Generate responses from your fine-tuned model."""
42
  full_prompt = system_message + "\n\n"
 
59
  )
60
 
61
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
62
+
63
+ # Extract only the assistant's last message
64
  if "Assistant:" in response:
65
  response = response.split("Assistant:")[-1].strip()
66
 
67
+ return response
68
 
69
 
70
+ # โœ… Create Gradio Chat Interface
71
+ chat_ui = gr.ChatInterface(
72
+ fn=lambda message, history, system_message, max_tokens, temperature, top_p: (
73
+ respond(message, history, system_message, max_tokens, temperature, top_p)
74
+ ),
75
  additional_inputs=[
76
  gr.Textbox(value="You are a helpful math assistant.", label="System message"),
77
  gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="Max new tokens"),
 
83
  )
84
 
85
 
86
+ # โœ… Add API endpoint `/chat` (for gradio_client access)
87
+ api_chat = gr.Interface(
88
+ fn=respond,
89
+ inputs=[
90
+ gr.Textbox(label="Message"),
91
+ gr.State(), # placeholder for chat history (can be None)
92
+ gr.Textbox(value="You are a helpful math assistant.", label="System message"),
93
+ gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="Max new tokens"),
94
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
95
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
96
+ ],
97
+ outputs="text",
98
+ api_name="/chat"
99
+ )
100
+
101
+
102
+ # โœ… Combine UI + API
103
+ demo = gr.TabbedInterface([chat_ui, api_chat], ["Chat", "API"])
104
+
105
+
106
  # โœ… Launch app
107
  if __name__ == "__main__":
108
  demo.launch()