Spaces:
Runtime error
Runtime error
File size: 7,953 Bytes
f284964 bddeb87 f284964 |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
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() |