Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
ACCESS_TOKEN = os.getenv("HF_TOKEN") | |
client = OpenAI( | |
base_url="https://api-inference.huggingface.co/v1/", | |
api_key=ACCESS_TOKEN, | |
) | |
def generate_python_program( | |
program_type, | |
difficulty, | |
features, | |
documentation_level, | |
optimization_focus, | |
output_format | |
): | |
feature_prompts = { | |
"Basic Functionality": "Include fundamental features and functionalities.", | |
"Intermediate Features": "Add modularity, error handling, and input validation.", | |
"Advanced Features": "Incorporate advanced libraries, multithreading, or optimization techniques.", | |
} | |
doc_prompts = { | |
"Minimal": "Provide minimal inline comments for understanding.", | |
"Moderate": "Include detailed comments explaining key parts of the code.", | |
"Comprehensive": "Provide full documentation, including docstrings and usage examples.", | |
} | |
optimization_prompts = { | |
"Readability": "Focus on writing clean and readable code.", | |
"Performance": "Prioritize optimizing performance and resource usage.", | |
"Scalability": "Ensure the code can handle large-scale use cases effectively.", | |
} | |
base_prompt = f""" | |
Act as an expert Python programmer and code generator. | |
Generate a Python program with the following requirements: | |
- Program Type: {program_type} | |
- Difficulty Level: {difficulty} | |
- Features: {feature_prompts[features]} | |
- Documentation Level: {doc_prompts[documentation_level]} | |
- Optimization Focus: {optimization_prompts[optimization_focus]} | |
- Output Format: {output_format} | |
Additional Requirements: | |
- Ensure code is executable and error-free. | |
- Include best practices for Python development. | |
- If libraries are required, mention installation instructions. | |
- Add a test case or example usage if applicable. | |
""" | |
try: | |
messages = [ | |
{"role": "system", "content": "You are an expert Python code generator."}, | |
{"role": "user", "content": base_prompt} | |
] | |
response = client.chat.completions.create( | |
model="Qwen/QwQ-32B-Preview", | |
messages=messages, | |
max_tokens=2048, | |
temperature=0.7, | |
top_p=0.9 | |
) | |
return response.choices[0].message.content | |
except Exception as e: | |
return f"An error occurred: {str(e)}\nPlease try again with different parameters." | |
def create_interface(): | |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as iface: | |
gr.Markdown(""" | |
# <div align="center"><strong>π οΈ Python Program Generator</strong></div> | |
Welcome to your personalized Python program generator! This tool allows you to: | |
- Create executable Python programs | |
- Customize features, difficulty, and optimization focus | |
- Receive well-documented and ready-to-run code | |
Unlock the power of Python programming today! π | |
""") | |
with gr.Row(): | |
with gr.Column(): | |
program_type = gr.Textbox( | |
label="Program Type", | |
placeholder="Describe the type of program (e.g., 'calculator', 'web scraper', 'data visualizer')", | |
lines=2 | |
) | |
difficulty = gr.Radio( | |
choices=["Beginner", "Intermediate", "Advanced"], | |
label="Difficulty Level", | |
value="Intermediate", | |
info="Choose based on the complexity of the program" | |
) | |
features = gr.Radio( | |
choices=[ | |
"Basic Functionality", | |
"Intermediate Features", | |
"Advanced Features" | |
], | |
label="Features", | |
value="Basic Functionality", | |
info="Select the level of features for your program" | |
) | |
documentation_level = gr.Radio( | |
choices=["Minimal", "Moderate", "Comprehensive"], | |
label="Documentation Level", | |
value="Moderate", | |
info="Choose the level of inline comments and documentation" | |
) | |
optimization_focus = gr.Radio( | |
choices=["Readability", "Performance", "Scalability"], | |
label="Optimization Focus", | |
value="Readability", | |
info="Specify the main focus for optimization" | |
) | |
output_format = gr.Radio( | |
choices=["Executable Code", "Code with Explanation", "Both"], | |
label="Output Format", | |
value="Both", | |
info="Choose how the output should be structured" | |
) | |
submit_btn = gr.Button( | |
"Generate Python Program", | |
variant="primary" | |
) | |
with gr.Column(): | |
output = gr.Textbox( | |
label="Generated Python Program", | |
lines=20, | |
show_copy_button=True | |
) | |
# Example scenarios | |
gr.Examples( | |
examples=[ | |
[ | |
"Calculator for basic arithmetic", | |
"Beginner", | |
"Basic Functionality", | |
"Minimal", | |
"Readability", | |
"Executable Code" | |
], | |
[ | |
"Web scraper for extracting data from websites", | |
"Intermediate", | |
"Intermediate Features", | |
"Moderate", | |
"Performance", | |
"Code with Explanation" | |
], | |
[ | |
"Machine Learning model trainer", | |
"Advanced", | |
"Advanced Features", | |
"Comprehensive", | |
"Scalability", | |
"Both" | |
] | |
], | |
inputs=[ | |
program_type, | |
difficulty, | |
features, | |
documentation_level, | |
optimization_focus, | |
output_format | |
], | |
outputs=output, | |
fn=generate_python_program, | |
cache_examples=True | |
) | |
# Usage tips | |
gr.Markdown(""" | |
### π‘ Tips for Best Results | |
1. **Be Specific** with your program description - instead of "Tool", try "File Organizer". | |
2. **Match the Difficulty** to your programming experience. | |
3. **Experiment with Features** to customize your program. | |
4. **Choose the Right Focus** for optimization based on your needs. | |
### π― Documentation Options | |
- **Minimal**: Quick and simple understanding. | |
- **Moderate**: Key parts of the code are well-explained. | |
- **Comprehensive**: Detailed docstrings and examples included. | |
### π Remember | |
- Test your code after generation. | |
- Modify the generated code for better personalization. | |
- Use the examples to explore various possibilities. | |
""") | |
submit_btn.click( | |
fn=generate_python_program, | |
inputs=[ | |
program_type, | |
difficulty, | |
features, | |
documentation_level, | |
optimization_focus, | |
output_format | |
], | |
outputs=output | |
) | |
return iface | |
if __name__ == "__main__": | |
iface = create_interface() | |
iface.launch() |