Vivek16 commited on
Commit
3f68147
·
verified ·
1 Parent(s): 0e38c17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -19
app.py CHANGED
@@ -2,23 +2,29 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
 
 
 
 
 
 
 
 
 
5
  def respond(
6
  message,
7
- history: list[dict[str, str]],
8
  system_message,
9
  max_tokens,
10
  temperature,
11
  top_p,
12
- hf_token: gr.OAuthToken,
13
  ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
-
19
  messages = [{"role": "system", "content": system_message}]
20
 
21
- messages.extend(history)
 
 
 
 
22
 
23
  messages.append({"role": "user", "content": message})
24
 
@@ -31,10 +37,7 @@ def respond(
31
  temperature=temperature,
32
  top_p=top_p,
33
  ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
 
39
  response += token
40
  yield response
@@ -43,9 +46,8 @@ def respond(
43
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
  """
46
- chatbot = gr.ChatInterface(
47
  respond,
48
- type="messages",
49
  additional_inputs=[
50
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
@@ -60,11 +62,37 @@ chatbot = gr.ChatInterface(
60
  ],
61
  )
62
 
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
67
-
68
 
69
  if __name__ == "__main__":
70
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from huggingface_hub import InferenceClient
3
 
4
 
5
+ """
6
+ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
7
+ """
8
+
9
+ import os
10
+ client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=os.getenv("HUGGINGFACEHUB_API_TOKEN"))
11
+
12
+
13
  def respond(
14
  message,
15
+ history: list[tuple[str, str]],
16
  system_message,
17
  max_tokens,
18
  temperature,
19
  top_p,
 
20
  ):
 
 
 
 
 
21
  messages = [{"role": "system", "content": system_message}]
22
 
23
+ for val in history:
24
+ if val[0]:
25
+ messages.append({"role": "user", "content": val[0]})
26
+ if val[1]:
27
+ messages.append({"role": "assistant", "content": val[1]})
28
 
29
  messages.append({"role": "user", "content": message})
30
 
 
37
  temperature=temperature,
38
  top_p=top_p,
39
  ):
40
+ token = message.choices[0].delta.content
 
 
 
41
 
42
  response += token
43
  yield response
 
46
  """
47
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
48
  """
49
+ demo = gr.ChatInterface(
50
  respond,
 
51
  additional_inputs=[
52
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
53
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
62
  ],
63
  )
64
 
 
 
 
 
 
65
 
66
  if __name__ == "__main__":
67
  demo.launch()
68
+
69
+ import os
70
+ from transformers import AutoModelForCausalLM, AutoTokenizer
71
+ from peft import PeftModel
72
+ import torch
73
+
74
+ # Load Hugging Face API token securely
75
+ api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
76
+
77
+ if not api_token:
78
+ raise ValueError("❌ ERROR: Hugging Face API token is not set. Please set it as an environment variable.")
79
+
80
+ # Define model names
81
+ base_model_name = "unsloth/qwen2.5-math-7b-bnb-4bit"
82
+ peft_model_name = "Hrushi02/Root_Math"
83
+
84
+ # Load base model with authentication
85
+ base_model = AutoModelForCausalLM.from_pretrained(
86
+ base_model_name,
87
+ torch_dtype=torch.float16,
88
+ device_map="auto",
89
+ use_auth_token=api_token # ✅ Correct
90
+ )
91
+
92
+
93
+ # Load fine-tuned model
94
+ model = PeftModel.from_pretrained(base_model, peft_model_name, token=api_token)
95
+
96
+ # Load tokenizer
97
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name, token=api_token)
98
+