Spaces:
Running
Running
Delete app.py.bak
Browse files- app.py.bak +0 -125
app.py.bak
DELETED
|
@@ -1,125 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
from peft import PeftModel
|
| 5 |
-
import gc
|
| 6 |
-
import uvicorn # β new
|
| 7 |
-
|
| 8 |
-
# Model configuration
|
| 9 |
-
MODEL_NAME = "robertnetwork/strudel-small"
|
| 10 |
-
MAX_LENGTH = 2048
|
| 11 |
-
|
| 12 |
-
# Global variables for model and tokenizer
|
| 13 |
-
model = None
|
| 14 |
-
tokenizer = None
|
| 15 |
-
|
| 16 |
-
def load_model():
|
| 17 |
-
"""Load the model and tokenizer"""
|
| 18 |
-
global model, tokenizer
|
| 19 |
-
|
| 20 |
-
print("Loading model and tokenizer...")
|
| 21 |
-
|
| 22 |
-
# Load tokenizer
|
| 23 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 24 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 25 |
-
tokenizer.padding_side = "left"
|
| 26 |
-
|
| 27 |
-
# Load model (CPU only for Spaces)
|
| 28 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 29 |
-
MODEL_NAME,
|
| 30 |
-
torch_dtype=torch.float32,
|
| 31 |
-
device_map="auto",
|
| 32 |
-
low_cpu_mem_usage=True
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
print("Model loaded successfully!")
|
| 36 |
-
|
| 37 |
-
def generate_code(instruction, style_input, max_new_tokens=512, temperature=0.7, top_p=0.9, do_sample=True):
|
| 38 |
-
"""Generate Strudel.cc JavaScript code based on style input"""
|
| 39 |
-
if model is None or tokenizer is None:
|
| 40 |
-
return "Error: Model not loaded. Please wait for the model to initialize."
|
| 41 |
-
try:
|
| 42 |
-
prompt = f"Instruction: {instruction}\nInput: {style_input}\nOutput:"
|
| 43 |
-
inputs = tokenizer(
|
| 44 |
-
prompt,
|
| 45 |
-
return_tensors="pt",
|
| 46 |
-
truncation=True,
|
| 47 |
-
max_length=MAX_LENGTH - max_new_tokens,
|
| 48 |
-
padding=True
|
| 49 |
-
)
|
| 50 |
-
with torch.no_grad():
|
| 51 |
-
outputs = model.generate(
|
| 52 |
-
**inputs,
|
| 53 |
-
max_new_tokens=min(max_new_tokens, 256),
|
| 54 |
-
temperature=temperature,
|
| 55 |
-
top_p=top_p,
|
| 56 |
-
do_sample=do_sample,
|
| 57 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 58 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 59 |
-
repetition_penalty=1.1,
|
| 60 |
-
early_stopping=True,
|
| 61 |
-
no_repeat_ngram_size=3
|
| 62 |
-
)
|
| 63 |
-
full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 64 |
-
if "Output:" in full_response:
|
| 65 |
-
generated_code = full_response.split("Output:")[-1].strip()
|
| 66 |
-
else:
|
| 67 |
-
generated_code = full_response.strip()
|
| 68 |
-
if len(generated_code) > 2000:
|
| 69 |
-
generated_code = generated_code[:2000] + "\n// ... (truncated)"
|
| 70 |
-
return generated_code
|
| 71 |
-
except Exception as e:
|
| 72 |
-
return f"Error generating code: {e}"
|
| 73 |
-
|
| 74 |
-
def gradio_interface(style_input, max_tokens, temperature, top_p, use_sampling):
|
| 75 |
-
instruction = "Generate Strudel.cc JavaScript code given style tags and a prompt, without commentary or markdown."
|
| 76 |
-
return generate_code(
|
| 77 |
-
instruction=instruction,
|
| 78 |
-
style_input=style_input,
|
| 79 |
-
max_new_tokens=int(max_tokens),
|
| 80 |
-
temperature=temperature,
|
| 81 |
-
top_p=top_p,
|
| 82 |
-
do_sample=use_sampling
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
-
# Build the Blocks UI
|
| 86 |
-
load_model()
|
| 87 |
-
with gr.Blocks(title="Strudel.cc Code Generator", theme=gr.themes.Soft()) as demo:
|
| 88 |
-
gr.Markdown("""
|
| 89 |
-
# π΅ Strudel.cc Code Generator
|
| 90 |
-
|
| 91 |
-
Generate Strudel.cc JavaScript code based on style descriptions or musical concepts.
|
| 92 |
-
|
| 93 |
-
**Model:** robertnetwork/strudel-small (Fine-tuned with LoRA)
|
| 94 |
-
""")
|
| 95 |
-
with gr.Row():
|
| 96 |
-
with gr.Column(scale=1, min_width=300):
|
| 97 |
-
style_input = gr.Textbox(label="Style/Prompt", placeholder="e.g., electronic, ambientβ¦", lines=3, value="electronic")
|
| 98 |
-
with gr.Accordion("Generation Settings", open=False):
|
| 99 |
-
max_tokens = gr.Slider(50, 256, value=128, step=32, label="Max New Tokens")
|
| 100 |
-
temperature = gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature")
|
| 101 |
-
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.1, label="Top-p")
|
| 102 |
-
use_sampling = gr.Checkbox(label="Use Sampling", value=True)
|
| 103 |
-
generate_btn = gr.Button("π΅ Generate Code", variant="primary", size="lg")
|
| 104 |
-
with gr.Column(scale=2):
|
| 105 |
-
output_code = gr.Code(label="Generated Strudel.cc Code", language="javascript", lines=20)
|
| 106 |
-
|
| 107 |
-
gr.Markdown("### π― Example Prompts")
|
| 108 |
-
examples = gr.Examples(
|
| 109 |
-
examples=[["electronic"], ["ambient drone"], ["techno beat"], ["drum and bass"], ["minimalist"], ["glitch hop"], ["house music"], ["experimental"]],
|
| 110 |
-
inputs=[style_input]
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
generate_btn.click(fn=gradio_interface, inputs=[style_input, max_tokens, temperature, top_p, use_sampling], outputs=[output_code])
|
| 114 |
-
style_input.submit (fn=gradio_interface, inputs=[style_input, max_tokens, temperature, top_p, use_sampling], outputs=[output_code])
|
| 115 |
-
|
| 116 |
-
# ββββββββββββββββ LAUNCH WITH UVIORN + HTTPOOLS ββββββββββββββββ
|
| 117 |
-
if __name__ == "__main__":
|
| 118 |
-
# demo.queue() # uncomment if you want background queuing support
|
| 119 |
-
uvicorn.run(
|
| 120 |
-
demo.app,
|
| 121 |
-
host="0.0.0.0",
|
| 122 |
-
port=7860,
|
| 123 |
-
http="httptools", # β switch off H11
|
| 124 |
-
ws="websockets"
|
| 125 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|