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

Update from Kaggle notebook

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