Spaces:
Runtime error
Runtime error
import gradio as gr | |
def process_input(user_input): | |
"""Process user input through the model and return the result.""" | |
messages = [{"role": "user", "content": user_input}] | |
# Apply chat template and generate response | |
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device) | |
outputs = model.generate(input_tensor, max_new_tokens=300, pad_token_id=tokenizer.eos_token_id) | |
result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True) | |
return result | |
# Create Gradio interface | |
demo = gr.Interface( | |
fn=process_input, | |
inputs=gr.Textbox(placeholder="Enter your equation (e.g. π₯ Γ· (π - π) = 2, π = 7, π = 3)"), | |
outputs=gr.Textbox(label="Model Output"), | |
title="Emoji Math Solver", | |
description="Enter a math equation with emojis, and the model will solve it." | |
) | |
demo.launch(share=True) | |
output_weights_path = "/kaggle/working/fine_tuned_deepseek_math_weights.pth" | |
torch.save(model.state_dict(), output_weights_path) | |
import shutil | |
shutil.make_archive("/kaggle/working/fine_tuned_deepseek_math_weights.pth", "zip", output_dir) | |
print("Model and tokenizer saved and zipped at /kaggle/working/weights.zip") | |
import os | |
from getpass import getpass | |
from huggingface_hub import HfApi, Repository | |
import re | |
# Get your Hugging Face token | |
hf_token = getpass("Enter your Hugging Face token: ") | |
api = HfApi(token=hf_token) | |
# Get your Space name (username/space-name) | |
space_name = input("Enter your Hugging Face Space name (username/space-name): ") | |
# Extract the Gradio code from your notebook | |
# This assumes your Gradio app is defined in a cell or cells in your notebook | |
from IPython import get_ipython | |
# Get all cells from the notebook | |
cells = get_ipython().user_ns.get('In', []) | |
# Extract cells that contain Gradio code | |
gradio_code = [] | |
in_gradio_block = False | |
for cell in cells: | |
# Look for cells that import gradio or define the interface | |
if 'import gradio' in cell or 'gr.Interface' in cell or in_gradio_block: | |
in_gradio_block = True | |
gradio_code.append(cell) | |
# If we find a cell that seems to end the Gradio app definition | |
elif in_gradio_block and ('if __name__' in cell or 'demo.launch()' in cell): | |
gradio_code.append(cell) | |
in_gradio_block = False | |
# Combine the code and ensure it has a launch method | |
combined_code = "\n\n".join(gradio_code) | |
# Make sure the app launches when run | |
if 'if __name__ == "__main__"' not in combined_code: | |
combined_code += '\n\nif __name__ == "__main__":\n demo.launch()' | |
# Save to app.py | |
with open("app.py", "w") as f: | |
f.write(combined_code) | |
print("Extracted Gradio code and saved to app.py") | |
# Clone the existing Space repository | |
repo = Repository( | |
local_dir="space_repo", | |
clone_from=f"https://huggingface.co/spaces/{space_name}", | |
token=hf_token, | |
git_user="marwashahid", | |
git_email="[email protected]" | |
) | |
# Copy app.py to the repository | |
import shutil | |
shutil.copy("app.py", "space_repo/app.py") | |
# Add requirements if needed | |
requirements = """ | |
gradio>=3.50.2 | |
""" | |
with open("space_repo/requirements.txt", "w") as f: | |
f.write(requirements) | |
# Commit and push changes | |
repo.git_add() | |
repo.git_commit("Update from Kaggle notebook") | |
repo.git_push() | |
print(f"Successfully deployed to https://huggingface.co/spaces/{space_name}") |