Commit
·
8b74289
1
Parent(s):
8b8f03d
Update from Kaggle notebook
Browse files
app.py
CHANGED
@@ -1,23 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
def process_input(user_input):
|
4 |
-
"""Process user input through the model and return the result."""
|
5 |
-
messages = [{"role": "user", "content": user_input}]
|
6 |
-
|
7 |
-
# Apply chat template and generate response
|
8 |
-
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
9 |
-
outputs = model.generate(input_tensor, max_new_tokens=300, pad_token_id=tokenizer.eos_token_id)
|
10 |
-
result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
|
11 |
-
|
12 |
-
return result
|
13 |
-
|
14 |
-
from peft import PeftModel
|
15 |
-
|
16 |
-
output_weights_path = "/kaggle/working/fine_tuned_deepseek_math_weights.pth"
|
17 |
-
torch.save(model.state_dict(), output_weights_path)
|
18 |
-
|
19 |
-
import gradio as gr
|
20 |
-
|
21 |
def process_input(user_input):
|
22 |
"""Process user input through the model and return the result."""
|
23 |
messages = [{"role": "user", "content": user_input}]
|
@@ -40,226 +22,8 @@ demo = gr.Interface(
|
|
40 |
|
41 |
demo.launch(share=True)
|
42 |
|
43 |
-
import os
|
44 |
-
from getpass import getpass
|
45 |
-
from huggingface_hub import HfApi, Repository
|
46 |
-
import re
|
47 |
-
|
48 |
-
# Get your Hugging Face token
|
49 |
-
hf_token = getpass("Enter your Hugging Face token: ")
|
50 |
-
api = HfApi(token=hf_token)
|
51 |
-
|
52 |
-
# Get your Space name (username/space-name)
|
53 |
-
space_name = input("Enter your Hugging Face Space name (username/space-name): ")
|
54 |
-
|
55 |
-
# Extract the Gradio code from your notebook
|
56 |
-
# This assumes your Gradio app is defined in a cell or cells in your notebook
|
57 |
-
from IPython import get_ipython
|
58 |
-
|
59 |
-
# Get all cells from the notebook
|
60 |
-
cells = get_ipython().user_ns.get('In', [])
|
61 |
-
|
62 |
-
# Extract cells that contain Gradio code
|
63 |
-
gradio_code = []
|
64 |
-
in_gradio_block = False
|
65 |
-
for cell in cells:
|
66 |
-
# Look for cells that import gradio or define the interface
|
67 |
-
if 'import gradio' in cell or 'gr.Interface' in cell or in_gradio_block:
|
68 |
-
in_gradio_block = True
|
69 |
-
gradio_code.append(cell)
|
70 |
-
# If we find a cell that seems to end the Gradio app definition
|
71 |
-
elif in_gradio_block and ('if __name__' in cell or 'demo.launch()' in cell):
|
72 |
-
gradio_code.append(cell)
|
73 |
-
in_gradio_block = False
|
74 |
-
|
75 |
-
# Combine the code and ensure it has a launch method
|
76 |
-
combined_code = "\n\n".join(gradio_code)
|
77 |
-
|
78 |
-
# Make sure the app launches when run
|
79 |
-
if 'if __name__ == "__main__"' not in combined_code:
|
80 |
-
combined_code += '\n\nif __name__ == "__main__":\n demo.launch()'
|
81 |
-
|
82 |
-
# Save to app.py
|
83 |
-
with open("app.py", "w") as f:
|
84 |
-
f.write(combined_code)
|
85 |
-
|
86 |
-
print("Extracted Gradio code and saved to app.py")
|
87 |
-
|
88 |
-
# Clone the existing Space repository
|
89 |
-
repo = Repository(
|
90 |
-
local_dir="space_repo",
|
91 |
-
clone_from=f"https://huggingface.co/spaces/{space_name}",
|
92 |
-
token=hf_token,
|
93 |
-
git_user="marwashahid",
|
94 |
-
git_email="[email protected]"
|
95 |
-
)
|
96 |
-
|
97 |
-
# Copy app.py to the repository
|
98 |
-
import shutil
|
99 |
-
shutil.copy("app.py", "space_repo/app.py")
|
100 |
-
|
101 |
-
# Add requirements if needed
|
102 |
-
requirements = """
|
103 |
-
gradio>=3.50.2
|
104 |
-
"""
|
105 |
-
with open("space_repo/requirements.txt", "w") as f:
|
106 |
-
f.write(requirements)
|
107 |
-
|
108 |
-
# Commit and push changes
|
109 |
-
repo.git_add()
|
110 |
-
repo.git_commit("Update from Kaggle notebook")
|
111 |
-
repo.git_push()
|
112 |
-
|
113 |
-
print(f"Successfully deployed to https://huggingface.co/spaces/{space_name}")
|
114 |
-
|
115 |
-
from peft import PeftModel
|
116 |
-
import os
|
117 |
-
from getpass import getpass
|
118 |
-
from huggingface_hub import HfApi, Repository
|
119 |
-
import re
|
120 |
-
|
121 |
-
# Get your Hugging Face token
|
122 |
-
hf_token = getpass("Enter your Hugging Face token: ")
|
123 |
-
api = HfApi(token=hf_token)
|
124 |
-
|
125 |
-
# Get your Space name (username/space-name)
|
126 |
-
space_name = input("Enter your Hugging Face Space name (username/space-name): ")
|
127 |
-
|
128 |
-
# Extract the Gradio code from your notebook
|
129 |
-
# This assumes your Gradio app is defined in a cell or cells in your notebook
|
130 |
-
from IPython import get_ipython
|
131 |
-
|
132 |
-
# Get all cells from the notebook
|
133 |
-
cells = get_ipython().user_ns.get('In', [])
|
134 |
-
|
135 |
-
# Extract cells that contain Gradio code
|
136 |
-
gradio_code = []
|
137 |
-
in_gradio_block = False
|
138 |
-
for cell in cells:
|
139 |
-
# Look for cells that import gradio or define the interface
|
140 |
-
if 'import gradio' in cell or 'gr.Interface' in cell or in_gradio_block:
|
141 |
-
in_gradio_block = True
|
142 |
-
gradio_code.append(cell)
|
143 |
-
# If we find a cell that seems to end the Gradio app definition
|
144 |
-
elif in_gradio_block and ('if __name__' in cell or 'demo.launch()' in cell):
|
145 |
-
gradio_code.append(cell)
|
146 |
-
in_gradio_block = False
|
147 |
-
|
148 |
-
# Combine the code and ensure it has a launch method
|
149 |
-
combined_code = "\n\n".join(gradio_code)
|
150 |
-
|
151 |
-
# Make sure the app launches when run
|
152 |
-
if 'if __name__ == "__main__"' not in combined_code:
|
153 |
-
combined_code += '\n\nif __name__ == "__main__":\n demo.launch()'
|
154 |
-
|
155 |
-
# Save to app.py
|
156 |
-
with open("app.py", "w") as f:
|
157 |
-
f.write(combined_code)
|
158 |
-
|
159 |
-
print("Extracted Gradio code and saved to app.py")
|
160 |
-
|
161 |
-
# Clone the existing Space repository
|
162 |
-
repo = Repository(
|
163 |
-
local_dir="space_repo",
|
164 |
-
clone_from=f"https://huggingface.co/spaces/{space_name}",
|
165 |
-
token=hf_token,
|
166 |
-
git_user="marwashahid",
|
167 |
-
git_email="[email protected]"
|
168 |
-
)
|
169 |
-
|
170 |
-
# Copy app.py to the repository
|
171 |
-
import shutil
|
172 |
-
shutil.copy("app.py", "space_repo/app.py")
|
173 |
-
|
174 |
-
# Add requirements if needed
|
175 |
-
requirements = """
|
176 |
-
gradio>=3.50.2
|
177 |
-
"""
|
178 |
-
with open("space_repo/requirements.txt", "w") as f:
|
179 |
-
f.write(requirements)
|
180 |
-
|
181 |
-
# Commit and push changes
|
182 |
-
repo.git_add()
|
183 |
-
repo.git_commit("Update from Kaggle notebook")
|
184 |
-
repo.git_push()
|
185 |
-
|
186 |
-
print(f"Successfully deployed to https://huggingface.co/spaces/{space_name}")
|
187 |
-
|
188 |
get_ipython().run_line_magic('pip', 'install peft')
|
189 |
|
190 |
-
from peft import PeftModel
|
191 |
-
import os
|
192 |
-
from getpass import getpass
|
193 |
-
from huggingface_hub import HfApi, Repository
|
194 |
-
import re
|
195 |
-
|
196 |
-
# Get your Hugging Face token
|
197 |
-
hf_token = getpass("Enter your Hugging Face token: ")
|
198 |
-
api = HfApi(token=hf_token)
|
199 |
-
|
200 |
-
# Get your Space name (username/space-name)
|
201 |
-
space_name = input("Enter your Hugging Face Space name (username/space-name): ")
|
202 |
-
|
203 |
-
# Extract the Gradio code from your notebook
|
204 |
-
# This assumes your Gradio app is defined in a cell or cells in your notebook
|
205 |
-
from IPython import get_ipython
|
206 |
-
|
207 |
-
# Get all cells from the notebook
|
208 |
-
cells = get_ipython().user_ns.get('In', [])
|
209 |
-
|
210 |
-
# Extract cells that contain Gradio code
|
211 |
-
gradio_code = []
|
212 |
-
in_gradio_block = False
|
213 |
-
for cell in cells:
|
214 |
-
# Look for cells that import gradio or define the interface
|
215 |
-
if 'import gradio' in cell or 'gr.Interface' in cell or in_gradio_block:
|
216 |
-
in_gradio_block = True
|
217 |
-
gradio_code.append(cell)
|
218 |
-
# If we find a cell that seems to end the Gradio app definition
|
219 |
-
elif in_gradio_block and ('if __name__' in cell or 'demo.launch()' in cell):
|
220 |
-
gradio_code.append(cell)
|
221 |
-
in_gradio_block = False
|
222 |
-
|
223 |
-
# Combine the code and ensure it has a launch method
|
224 |
-
combined_code = "\n\n".join(gradio_code)
|
225 |
-
|
226 |
-
# Make sure the app launches when run
|
227 |
-
if 'if __name__ == "__main__"' not in combined_code:
|
228 |
-
combined_code += '\n\nif __name__ == "__main__":\n demo.launch()'
|
229 |
-
|
230 |
-
# Save to app.py
|
231 |
-
with open("app.py", "w") as f:
|
232 |
-
f.write(combined_code)
|
233 |
-
|
234 |
-
print("Extracted Gradio code and saved to app.py")
|
235 |
-
|
236 |
-
# Clone the existing Space repository
|
237 |
-
repo = Repository(
|
238 |
-
local_dir="space_repo",
|
239 |
-
clone_from=f"https://huggingface.co/spaces/{space_name}",
|
240 |
-
token=hf_token,
|
241 |
-
git_user="marwashahid",
|
242 |
-
git_email="[email protected]"
|
243 |
-
)
|
244 |
-
|
245 |
-
# Copy app.py to the repository
|
246 |
-
import shutil
|
247 |
-
shutil.copy("app.py", "space_repo/app.py")
|
248 |
-
|
249 |
-
# Add requirements if needed
|
250 |
-
requirements = """
|
251 |
-
gradio>=3.50.2
|
252 |
-
"""
|
253 |
-
with open("space_repo/requirements.txt", "w") as f:
|
254 |
-
f.write(requirements)
|
255 |
-
|
256 |
-
# Commit and push changes
|
257 |
-
repo.git_add()
|
258 |
-
repo.git_commit("Update from Kaggle notebook")
|
259 |
-
repo.git_push()
|
260 |
-
|
261 |
-
print(f"Successfully deployed to https://huggingface.co/spaces/{space_name}")
|
262 |
-
|
263 |
from peft import PeftModel
|
264 |
|
265 |
import os
|
|
|
1 |
import gradio as gr
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
def process_input(user_input):
|
4 |
"""Process user input through the model and return the result."""
|
5 |
messages = [{"role": "user", "content": user_input}]
|
|
|
22 |
|
23 |
demo.launch(share=True)
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
get_ipython().run_line_magic('pip', 'install peft')
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
from peft import PeftModel
|
28 |
|
29 |
import os
|