marwashahid commited on
Commit
8b8f03d
·
1 Parent(s): a479704

Update from Kaggle notebook

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py CHANGED
@@ -258,4 +258,78 @@ 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}")
 
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
266
+ from getpass import getpass
267
+ from huggingface_hub import HfApi, Repository
268
+ import re
269
+
270
+ # Get your Hugging Face token
271
+ hf_token = getpass("Enter your Hugging Face token: ")
272
+ api = HfApi(token=hf_token)
273
+
274
+ # Get your Space name (username/space-name)
275
+ space_name = input("Enter your Hugging Face Space name (username/space-name): ")
276
+
277
+ # Extract the Gradio code from your notebook
278
+ # This assumes your Gradio app is defined in a cell or cells in your notebook
279
+ from IPython import get_ipython
280
+
281
+ # Get all cells from the notebook
282
+ cells = get_ipython().user_ns.get('In', [])
283
+
284
+ # Extract cells that contain Gradio code
285
+ gradio_code = []
286
+ in_gradio_block = False
287
+ for cell in cells:
288
+ # Look for cells that import gradio or define the interface
289
+ if 'import gradio' in cell or 'gr.Interface' in cell or in_gradio_block:
290
+ in_gradio_block = True
291
+ gradio_code.append(cell)
292
+ # If we find a cell that seems to end the Gradio app definition
293
+ elif in_gradio_block and ('if __name__' in cell or 'demo.launch()' in cell):
294
+ gradio_code.append(cell)
295
+ in_gradio_block = False
296
+
297
+ # Combine the code and ensure it has a launch method
298
+ combined_code = "\n\n".join(gradio_code)
299
+
300
+ # Make sure the app launches when run
301
+ if 'if __name__ == "__main__"' not in combined_code:
302
+ combined_code += '\n\nif __name__ == "__main__":\n demo.launch()'
303
+
304
+ # Save to app.py
305
+ with open("app.py", "w") as f:
306
+ f.write(combined_code)
307
+
308
+ print("Extracted Gradio code and saved to app.py")
309
+
310
+ # Clone the existing Space repository
311
+ repo = Repository(
312
+ local_dir="space_repo",
313
+ clone_from=f"https://huggingface.co/spaces/{space_name}",
314
+ token=hf_token,
315
+ git_user="marwashahid",
316
+ git_email="[email protected]"
317
+ )
318
+
319
+ # Copy app.py to the repository
320
+ import shutil
321
+ shutil.copy("app.py", "space_repo/app.py")
322
+
323
+ # Add requirements if needed
324
+ requirements = """
325
+ gradio>=3.50.2
326
+ """
327
+ with open("space_repo/requirements.txt", "w") as f:
328
+ f.write(requirements)
329
+
330
+ # Commit and push changes
331
+ repo.git_add()
332
+ repo.git_commit("Update from Kaggle notebook")
333
+ repo.git_push()
334
+
335
  print(f"Successfully deployed to https://huggingface.co/spaces/{space_name}")