Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load a pre-trained code generation model (replace with your actual model)
|
5 |
+
code_generator = pipeline("text-generation", model="gpt-3.5-turbo") # Example model
|
6 |
+
|
7 |
+
# Function to generate code
|
8 |
+
def generate_code(prompt, file_type):
|
9 |
+
if file_type in ["Gradio", "Vercel", "Streamlit"]:
|
10 |
+
# Handle platform-specific code generation
|
11 |
+
prompt_with_file_type = f"Write a configuration or setup code for {file_type} to: {prompt}"
|
12 |
+
else:
|
13 |
+
# Handle programming language code generation
|
14 |
+
prompt_with_file_type = f"Write a {file_type} code for: {prompt}"
|
15 |
+
|
16 |
+
# Generate code using the AI model
|
17 |
+
generated_code = code_generator(prompt_with_file_type, max_length=200, num_return_sequences=1)
|
18 |
+
return generated_code[0]['generated_text']
|
19 |
+
|
20 |
+
# Function to update code
|
21 |
+
def update_code(existing_code, update_prompt):
|
22 |
+
# Combine the existing code and the update prompt
|
23 |
+
prompt_with_update = f"Rewrite the following code to: {update_prompt}\n\nExisting Code:\n{existing_code}"
|
24 |
+
|
25 |
+
# Generate updated code using the AI model
|
26 |
+
updated_code = code_generator(prompt_with_update, max_length=200, num_return_sequences=1)
|
27 |
+
return updated_code[0]['generated_text']
|
28 |
+
|
29 |
+
# Examples for the interface
|
30 |
+
examples = [
|
31 |
+
["Write a function to calculate factorial", "Python"],
|
32 |
+
["Create a simple interface for a calculator", "Gradio"],
|
33 |
+
["Deploy a Next.js app", "Vercel"],
|
34 |
+
["Create a data visualization app", "Streamlit"],
|
35 |
+
["Write a program to reverse a string", "JavaScript"],
|
36 |
+
["Create a responsive navbar", "HTML"],
|
37 |
+
]
|
38 |
+
|
39 |
+
# Gradio interface
|
40 |
+
with gr.Blocks(theme='Nymbo/Nymbo_Theme') as demo:
|
41 |
+
gr.Markdown("# AI Code Generator with Update Feature")
|
42 |
+
gr.Markdown("Enter a prompt and select the file type or platform to generate code. You can also update the generated code with a new prompt.")
|
43 |
+
|
44 |
+
with gr.Row():
|
45 |
+
input_prompt = gr.Textbox(label="Input Prompt", placeholder="e.g., Write a function to calculate factorial...")
|
46 |
+
file_type = gr.Dropdown(
|
47 |
+
label="File Type / Platform",
|
48 |
+
choices=["Python", "JavaScript", "HTML", "CSS", "Java", "C++", "Gradio", "Vercel", "Streamlit"],
|
49 |
+
value="Python"
|
50 |
+
)
|
51 |
+
|
52 |
+
output_code = gr.Textbox(label="Generated Code", lines=10, interactive=False)
|
53 |
+
|
54 |
+
generate_button = gr.Button("Generate Code")
|
55 |
+
generate_button.click(fn=generate_code, inputs=[input_prompt, file_type], outputs=output_code)
|
56 |
+
|
57 |
+
# Update Code Section
|
58 |
+
with gr.Row():
|
59 |
+
update_prompt = gr.Textbox(label="Update Prompt", placeholder="e.g., Add error handling to the code...")
|
60 |
+
update_button = gr.Button("Update Code")
|
61 |
+
|
62 |
+
update_button.click(fn=update_code, inputs=[output_code, update_prompt], outputs=output_code)
|
63 |
+
|
64 |
+
# Add examples
|
65 |
+
gr.Examples(
|
66 |
+
examples=examples,
|
67 |
+
inputs=[input_prompt, file_type],
|
68 |
+
outputs=output_code,
|
69 |
+
fn=generate_code,
|
70 |
+
cache_examples=True, # Cache results for faster loading
|
71 |
+
label="Click on an example to get started!"
|
72 |
+
)
|
73 |
+
|
74 |
+
# Launch the interface
|
75 |
+
demo.launch()
|