Spaces:
Runtime error
Runtime error
Commit
Β·
dd5eeda
1
Parent(s):
86552cd
Update from Kaggle notebook
Browse files- app.py +103 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Create Gradio interface
|
15 |
+
demo = gr.Interface(
|
16 |
+
fn=process_input,
|
17 |
+
inputs=gr.Textbox(placeholder="Enter your equation (e.g. π₯ Γ· (π - π) = 2, π = 7, π = 3)"),
|
18 |
+
outputs=gr.Textbox(label="Model Output"),
|
19 |
+
title="Emoji Math Solver",
|
20 |
+
description="Enter a math equation with emojis, and the model will solve it."
|
21 |
+
)
|
22 |
+
|
23 |
+
demo.launch(share=True)
|
24 |
+
|
25 |
+
output_weights_path = "/kaggle/working/fine_tuned_deepseek_math_weights.pth"
|
26 |
+
torch.save(model.state_dict(), output_weights_path)
|
27 |
+
|
28 |
+
|
29 |
+
import shutil
|
30 |
+
shutil.make_archive("/kaggle/working/fine_tuned_deepseek_math_weights.pth", "zip", output_dir)
|
31 |
+
print("Model and tokenizer saved and zipped at /kaggle/working/weights.zip")
|
32 |
+
|
33 |
+
import os
|
34 |
+
from getpass import getpass
|
35 |
+
from huggingface_hub import HfApi, Repository
|
36 |
+
import re
|
37 |
+
|
38 |
+
# Get your Hugging Face token
|
39 |
+
hf_token = getpass("Enter your Hugging Face token: ")
|
40 |
+
api = HfApi(token=hf_token)
|
41 |
+
|
42 |
+
# Get your Space name (username/space-name)
|
43 |
+
space_name = input("Enter your Hugging Face Space name (username/space-name): ")
|
44 |
+
|
45 |
+
# Extract the Gradio code from your notebook
|
46 |
+
# This assumes your Gradio app is defined in a cell or cells in your notebook
|
47 |
+
from IPython import get_ipython
|
48 |
+
|
49 |
+
# Get all cells from the notebook
|
50 |
+
cells = get_ipython().user_ns.get('In', [])
|
51 |
+
|
52 |
+
# Extract cells that contain Gradio code
|
53 |
+
gradio_code = []
|
54 |
+
in_gradio_block = False
|
55 |
+
for cell in cells:
|
56 |
+
# Look for cells that import gradio or define the interface
|
57 |
+
if 'import gradio' in cell or 'gr.Interface' in cell or in_gradio_block:
|
58 |
+
in_gradio_block = True
|
59 |
+
gradio_code.append(cell)
|
60 |
+
# If we find a cell that seems to end the Gradio app definition
|
61 |
+
elif in_gradio_block and ('if __name__' in cell or 'demo.launch()' in cell):
|
62 |
+
gradio_code.append(cell)
|
63 |
+
in_gradio_block = False
|
64 |
+
|
65 |
+
# Combine the code and ensure it has a launch method
|
66 |
+
combined_code = "\n\n".join(gradio_code)
|
67 |
+
|
68 |
+
# Make sure the app launches when run
|
69 |
+
if 'if __name__ == "__main__"' not in combined_code:
|
70 |
+
combined_code += '\n\nif __name__ == "__main__":\n demo.launch()'
|
71 |
+
|
72 |
+
# Save to app.py
|
73 |
+
with open("app.py", "w") as f:
|
74 |
+
f.write(combined_code)
|
75 |
+
|
76 |
+
print("Extracted Gradio code and saved to app.py")
|
77 |
+
|
78 |
+
# Clone the existing Space repository
|
79 |
+
repo = Repository(
|
80 |
+
local_dir="space_repo",
|
81 |
+
clone_from=f"https://huggingface.co/spaces/{space_name}",
|
82 |
+
token=hf_token,
|
83 |
+
git_user="marwashahid",
|
84 |
+
git_email="[email protected]"
|
85 |
+
)
|
86 |
+
|
87 |
+
# Copy app.py to the repository
|
88 |
+
import shutil
|
89 |
+
shutil.copy("app.py", "space_repo/app.py")
|
90 |
+
|
91 |
+
# Add requirements if needed
|
92 |
+
requirements = """
|
93 |
+
gradio>=3.50.2
|
94 |
+
"""
|
95 |
+
with open("space_repo/requirements.txt", "w") as f:
|
96 |
+
f.write(requirements)
|
97 |
+
|
98 |
+
# Commit and push changes
|
99 |
+
repo.git_add()
|
100 |
+
repo.git_commit("Update from Kaggle notebook")
|
101 |
+
repo.git_push()
|
102 |
+
|
103 |
+
print(f"Successfully deployed to https://huggingface.co/spaces/{space_name}")
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
|
2 |
+
gradio>=3.50.2
|