Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Initialize the text generation
|
| 5 |
pipe = pipeline("text-generation", model="akhaliq/MyGemmaGradioCoder")
|
|
|
|
| 6 |
|
| 7 |
-
def generate_code(user_input):
|
| 8 |
"""
|
| 9 |
-
Generate code based on user input using the Gemma model
|
| 10 |
"""
|
| 11 |
messages = [
|
| 12 |
{"role": "user", "content": user_input},
|
| 13 |
]
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# Generate response from the model
|
| 16 |
-
response =
|
| 17 |
|
| 18 |
# Extract the generated text from the response
|
| 19 |
generated_text = response[0]['generated_text']
|
|
@@ -32,14 +36,25 @@ def generate_code(user_input):
|
|
| 32 |
return generated_text.split(user_input)[-1].strip()
|
| 33 |
return generated_text
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Create the Gradio interface
|
| 36 |
-
with gr.Blocks(title="Text to Code Generator", theme=gr.themes.Soft()) as demo:
|
| 37 |
gr.Markdown(
|
| 38 |
"""
|
| 39 |
-
# π Text to Code Generator
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
Simply describe what you want to build, and the AI will generate the corresponding code!
|
| 43 |
"""
|
| 44 |
)
|
| 45 |
|
|
@@ -54,7 +69,7 @@ with gr.Blocks(title="Text to Code Generator", theme=gr.themes.Soft()) as demo:
|
|
| 54 |
)
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
-
generate_btn = gr.Button("Generate
|
| 58 |
clear_btn = gr.ClearButton([input_text], value="Clear", scale=1)
|
| 59 |
|
| 60 |
# Examples section
|
|
@@ -70,44 +85,71 @@ with gr.Blocks(title="Text to Code Generator", theme=gr.themes.Soft()) as demo:
|
|
| 70 |
label="Example Prompts"
|
| 71 |
)
|
| 72 |
|
| 73 |
-
with gr.Column(scale=
|
| 74 |
-
# Output section
|
| 75 |
-
output_code = gr.Code(
|
| 76 |
-
label="Generated Code",
|
| 77 |
-
language="python",
|
| 78 |
-
lines=20,
|
| 79 |
-
interactive=True,
|
| 80 |
-
show_line_numbers=True,
|
| 81 |
-
wrap_lines=True,
|
| 82 |
-
autocomplete=True
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
with gr.Row():
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Add event handlers
|
| 89 |
generate_btn.click(
|
| 90 |
-
fn=
|
| 91 |
inputs=input_text,
|
| 92 |
-
outputs=
|
| 93 |
api_name="generate"
|
| 94 |
)
|
| 95 |
|
| 96 |
input_text.submit(
|
| 97 |
-
fn=
|
| 98 |
inputs=input_text,
|
| 99 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
copy_btn.click(
|
| 104 |
fn=None,
|
| 105 |
-
inputs=
|
| 106 |
outputs=None,
|
| 107 |
js="""
|
| 108 |
(code) => {
|
| 109 |
navigator.clipboard.writeText(code);
|
| 110 |
-
alert('Code copied to clipboard!');
|
| 111 |
return null;
|
| 112 |
}
|
| 113 |
"""
|
|
@@ -122,7 +164,9 @@ with gr.Blocks(title="Text to Code Generator", theme=gr.themes.Soft()) as demo:
|
|
| 122 |
- Include details about inputs, outputs, and edge cases
|
| 123 |
- You can edit the generated code directly in the output box
|
| 124 |
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
"""
|
| 127 |
)
|
| 128 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Initialize the text generation pipelines
|
| 5 |
pipe = pipeline("text-generation", model="akhaliq/MyGemmaGradioCoder")
|
| 6 |
+
pipe2 = pipeline("text-generation", model="google/gemma-3-270m-it")
|
| 7 |
|
| 8 |
+
def generate_code(user_input, model_choice="Model 1"):
|
| 9 |
"""
|
| 10 |
+
Generate code based on user input using the selected Gemma model
|
| 11 |
"""
|
| 12 |
messages = [
|
| 13 |
{"role": "user", "content": user_input},
|
| 14 |
]
|
| 15 |
|
| 16 |
+
# Select pipeline based on model choice
|
| 17 |
+
selected_pipe = pipe if model_choice == "Model 1 (MyGemmaGradioCoder)" else pipe2
|
| 18 |
+
|
| 19 |
# Generate response from the model
|
| 20 |
+
response = selected_pipe(messages, max_new_tokens=512, temperature=0.7, do_sample=True)
|
| 21 |
|
| 22 |
# Extract the generated text from the response
|
| 23 |
generated_text = response[0]['generated_text']
|
|
|
|
| 36 |
return generated_text.split(user_input)[-1].strip()
|
| 37 |
return generated_text
|
| 38 |
|
| 39 |
+
def generate_both(user_input):
|
| 40 |
+
"""
|
| 41 |
+
Generate code from both models for comparison
|
| 42 |
+
"""
|
| 43 |
+
output1 = generate_code(user_input, "Model 1 (MyGemmaGradioCoder)")
|
| 44 |
+
output2 = generate_code(user_input, "Model 2 (gemma-3-270m-it)")
|
| 45 |
+
return output1, output2
|
| 46 |
+
|
| 47 |
# Create the Gradio interface
|
| 48 |
+
with gr.Blocks(title="Text to Code Generator - Model Comparison", theme=gr.themes.Soft()) as demo:
|
| 49 |
gr.Markdown(
|
| 50 |
"""
|
| 51 |
+
# π Text to Code Generator - Model Comparison
|
| 52 |
+
|
| 53 |
+
Compare code generation from two different Gemma models:
|
| 54 |
+
- **Model 1**: akhaliq/MyGemmaGradioCoder
|
| 55 |
+
- **Model 2**: google/gemma-3-270m-it
|
| 56 |
|
| 57 |
+
Simply describe what you want to build, and see how each model responds!
|
|
|
|
| 58 |
"""
|
| 59 |
)
|
| 60 |
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
+
generate_btn = gr.Button("Generate from Both Models", variant="primary", scale=2)
|
| 73 |
clear_btn = gr.ClearButton([input_text], value="Clear", scale=1)
|
| 74 |
|
| 75 |
# Examples section
|
|
|
|
| 85 |
label="Example Prompts"
|
| 86 |
)
|
| 87 |
|
| 88 |
+
with gr.Column(scale=2):
|
| 89 |
+
# Output section - Two columns for comparison
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
with gr.Row():
|
| 91 |
+
with gr.Column():
|
| 92 |
+
gr.Markdown("### Model 1: MyGemmaGradioCoder")
|
| 93 |
+
output_code1 = gr.Code(
|
| 94 |
+
label="Generated Code (Model 1)",
|
| 95 |
+
language="python",
|
| 96 |
+
lines=15,
|
| 97 |
+
interactive=True,
|
| 98 |
+
show_line_numbers=True,
|
| 99 |
+
wrap_lines=True,
|
| 100 |
+
autocomplete=True
|
| 101 |
+
)
|
| 102 |
+
copy_btn1 = gr.Button("π Copy Code", scale=1)
|
| 103 |
+
|
| 104 |
+
with gr.Column():
|
| 105 |
+
gr.Markdown("### Model 2: gemma-3-270m-it")
|
| 106 |
+
output_code2 = gr.Code(
|
| 107 |
+
label="Generated Code (Model 2)",
|
| 108 |
+
language="python",
|
| 109 |
+
lines=15,
|
| 110 |
+
interactive=True,
|
| 111 |
+
show_line_numbers=True,
|
| 112 |
+
wrap_lines=True,
|
| 113 |
+
autocomplete=True
|
| 114 |
+
)
|
| 115 |
+
copy_btn2 = gr.Button("π Copy Code", scale=1)
|
| 116 |
|
| 117 |
# Add event handlers
|
| 118 |
generate_btn.click(
|
| 119 |
+
fn=generate_both,
|
| 120 |
inputs=input_text,
|
| 121 |
+
outputs=[output_code1, output_code2],
|
| 122 |
api_name="generate"
|
| 123 |
)
|
| 124 |
|
| 125 |
input_text.submit(
|
| 126 |
+
fn=generate_both,
|
| 127 |
inputs=input_text,
|
| 128 |
+
outputs=[output_code1, output_code2]
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# Copy functionality for both outputs
|
| 132 |
+
copy_btn1.click(
|
| 133 |
+
fn=None,
|
| 134 |
+
inputs=output_code1,
|
| 135 |
+
outputs=None,
|
| 136 |
+
js="""
|
| 137 |
+
(code) => {
|
| 138 |
+
navigator.clipboard.writeText(code);
|
| 139 |
+
alert('Code from Model 1 copied to clipboard!');
|
| 140 |
+
return null;
|
| 141 |
+
}
|
| 142 |
+
"""
|
| 143 |
)
|
| 144 |
|
| 145 |
+
copy_btn2.click(
|
|
|
|
| 146 |
fn=None,
|
| 147 |
+
inputs=output_code2,
|
| 148 |
outputs=None,
|
| 149 |
js="""
|
| 150 |
(code) => {
|
| 151 |
navigator.clipboard.writeText(code);
|
| 152 |
+
alert('Code from Model 2 copied to clipboard!');
|
| 153 |
return null;
|
| 154 |
}
|
| 155 |
"""
|
|
|
|
| 164 |
- Include details about inputs, outputs, and edge cases
|
| 165 |
- You can edit the generated code directly in the output box
|
| 166 |
|
| 167 |
+
**Models:**
|
| 168 |
+
- [akhaliq/MyGemmaGradioCoder](https://huggingface.co/akhaliq/MyGemmaGradioCoder)
|
| 169 |
+
- [google/gemma-3-270m-it](https://huggingface.co/google/gemma-3-270m-it)
|
| 170 |
"""
|
| 171 |
)
|
| 172 |
|