marwashahid commited on
Commit
a479704
·
1 Parent(s): 818fbfe

Update from Kaggle notebook

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py CHANGED
@@ -183,4 +183,79 @@ 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}")
 
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}")