marwashahid's picture
Update from Kaggle notebook
85e3839
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)
get_ipython().run_line_magic('pip', 'install peft')
from peft import PeftModel
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}")
# Create Gradio interface
demo = gr.Interface(
fn=process_input,
inputs=gr.Textbox(placeholder="Enter your equation:"),
outputs=gr.Textbox(label="Model Output"),
title="Math Problem Solver",
description="Enter a math equation with emojis, and the model will solve it."
)
demo.launch(share=True)
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}")