File size: 3,155 Bytes
79e3005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a479704
 
8b8f03d
 
 
 
 
 
79e3005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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}")