marwashahid commited on
Commit
85e3839
·
1 Parent(s): 8b74289

Update from Kaggle notebook

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py CHANGED
@@ -96,4 +96,87 @@ repo.git_add()
96
  repo.git_commit("Update from Kaggle notebook")
97
  repo.git_push()
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  print(f"Successfully deployed to https://huggingface.co/spaces/{space_name}")
 
96
  repo.git_commit("Update from Kaggle notebook")
97
  repo.git_push()
98
 
99
+ print(f"Successfully deployed to https://huggingface.co/spaces/{space_name}")
100
+
101
+ # Create Gradio interface
102
+ demo = gr.Interface(
103
+ fn=process_input,
104
+ inputs=gr.Textbox(placeholder="Enter your equation:"),
105
+ outputs=gr.Textbox(label="Model Output"),
106
+ title="Math Problem Solver",
107
+ description="Enter a math equation with emojis, and the model will solve it."
108
+ )
109
+
110
+ demo.launch(share=True)
111
+
112
+ import os
113
+ from getpass import getpass
114
+ from huggingface_hub import HfApi, Repository
115
+ import re
116
+
117
+ # Get your Hugging Face token
118
+ hf_token = getpass("Enter your Hugging Face token: ")
119
+ api = HfApi(token=hf_token)
120
+
121
+ # Get your Space name (username/space-name)
122
+ space_name = input("Enter your Hugging Face Space name (username/space-name): ")
123
+
124
+ # Extract the Gradio code from your notebook
125
+ # This assumes your Gradio app is defined in a cell or cells in your notebook
126
+ from IPython import get_ipython
127
+
128
+ # Get all cells from the notebook
129
+ cells = get_ipython().user_ns.get('In', [])
130
+
131
+ # Extract cells that contain Gradio code
132
+ gradio_code = []
133
+ in_gradio_block = False
134
+ for cell in cells:
135
+ # Look for cells that import gradio or define the interface
136
+ if 'import gradio' in cell or 'gr.Interface' in cell or in_gradio_block:
137
+ in_gradio_block = True
138
+ gradio_code.append(cell)
139
+ # If we find a cell that seems to end the Gradio app definition
140
+ elif in_gradio_block and ('if __name__' in cell or 'demo.launch()' in cell):
141
+ gradio_code.append(cell)
142
+ in_gradio_block = False
143
+
144
+ # Combine the code and ensure it has a launch method
145
+ combined_code = "\n\n".join(gradio_code)
146
+
147
+ # Make sure the app launches when run
148
+ if 'if __name__ == "__main__"' not in combined_code:
149
+ combined_code += '\n\nif __name__ == "__main__":\n demo.launch()'
150
+
151
+ # Save to app.py
152
+ with open("app.py", "w") as f:
153
+ f.write(combined_code)
154
+
155
+ print("Extracted Gradio code and saved to app.py")
156
+
157
+ # Clone the existing Space repository
158
+ repo = Repository(
159
+ local_dir="space_repo",
160
+ clone_from=f"https://huggingface.co/spaces/{space_name}",
161
+ token=hf_token,
162
+ git_user="marwashahid",
163
+ git_email="[email protected]"
164
+ )
165
+
166
+ # Copy app.py to the repository
167
+ import shutil
168
+ shutil.copy("app.py", "space_repo/app.py")
169
+
170
+ # Add requirements if needed
171
+ requirements = """
172
+ gradio>=3.50.2
173
+ """
174
+ with open("space_repo/requirements.txt", "w") as f:
175
+ f.write(requirements)
176
+
177
+ # Commit and push changes
178
+ repo.git_add()
179
+ repo.git_commit("Update from Kaggle notebook")
180
+ repo.git_push()
181
+
182
  print(f"Successfully deployed to https://huggingface.co/spaces/{space_name}")