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EN = os.getenv("HF_TOKEN")
|
6 |
+
|
7 |
+
client = OpenAI(
|
8 |
+
base_url="https://api-inference.huggingface.co/v1/",
|
9 |
+
api_key=ACCESS_TOKEN,
|
10 |
+
)
|
11 |
+
|
12 |
+
def generate_python_program(
|
13 |
+
program_type,
|
14 |
+
difficulty,
|
15 |
+
features,
|
16 |
+
documentation_level,
|
17 |
+
optimization_focus,
|
18 |
+
output_format
|
19 |
+
):
|
20 |
+
β # ΨͺΩΨΆΫΨΨ§Ψͺ ΩΫΪΪ―ΫβΩΨ§ Ψ¨Ψ±Ψ§Ϋ ΨͺΩΩΫΨ― Ϊ©Ψ―
|
21 |
+
feature_prompts = {
|
22 |
+
"Basic Functionality": "Include fundamental features and functionalities.",
|
23 |
+
"Intermediate Features": "Add modularity, error handling, and input validation.",
|
24 |
+
"Advanced Features": "Incorporate advanced libraries, multithreading, or optimization techniques.",
|
25 |
+
}
|
26 |
+
|
27 |
+
doc_prompts = {
|
28 |
+
"Minimal": "Provide minimal inline comments for understanding.",
|
29 |
+
"Moderate": "Include detailed comments explaining key parts of the code.",
|
30 |
+
"Comprehensive": "Provide full documentation, including docstrings and usage examples.",
|
31 |
+
}
|
32 |
+
|
33 |
+
optimization_prompts = {
|
34 |
+
"Readability": "Focus on writing clean and readable code.",
|
35 |
+
"Performance": "Prioritize optimizing performance and resource usage.",
|
36 |
+
"Scalability": "Ensure the code can handle large-scale use cases effectively.",
|
37 |
+
}
|
38 |
+
|
39 |
+
base_prompt = f"""
|
40 |
+
Act as an expert Python programmer and code generator.
|
41 |
+
Generate a Python program with the following requirements:
|
42 |
+
- Program Type: {program_type}
|
43 |
+
- Difficulty Level: {difficulty}
|
44 |
+
- Features: {feature_prompts[features]}
|
45 |
+
- Documentation Level: {doc_prompts[documentation_level]}
|
46 |
+
- Optimization Focus: {optimization_prompts[optimization_focus]}
|
47 |
+
- Output Format: {output_format}
|
48 |
+
|
49 |
+
Additional Requirements:
|
50 |
+
- Ensure code is executable and error-free.
|
51 |
+
- Include best practices for Python development.
|
52 |
+
- If libraries are required, mention installation instructions.
|
53 |
+
- Add a test case or example usage if applicable.
|
54 |
+
"""
|
55 |
+
|
56 |
+
try:
|
57 |
+
messages = [
|
58 |
+
{"role": "system", "content": "You are an expert Python code generator."},
|
59 |
+
{"role": "user", "content": base_prompt}
|
60 |
+
]
|
61 |
+
|
62 |
+
response = client.chat.completions.create(
|
63 |
+
model="Qwen/QwQ-32B-Preview",
|
64 |
+
messages=messages,
|
65 |
+
max_tokens=2048,
|
66 |
+
temperature=0.7,
|
67 |
+
top_p=0.9
|
68 |
+
)
|
69 |
+
|
70 |
+
return response.choices[0].message.content
|
71 |
+
|
72 |
+
except Exception as e:
|
73 |
+
return f"An error occurred: {str(e)}\nPlease try again with different parameters."
|
74 |
+
|
75 |
+
def create_interface():
|
76 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as iface:
|
77 |
+
gr.Markdown("""
|
78 |
+
# <div align="center"><strong>π οΈ Python Program Generator</strong></div>
|
79 |
+
|
80 |
+
Welcome to your personalized Python program generator! This tool allows you to:
|
81 |
+
- Create executable Python programs
|
82 |
+
- Customize features, difficulty, and optimization focus
|
83 |
+
- Receive well-documented and ready-to-run code
|
84 |
+
|
85 |
+
Unlock the power of Python programming today! π
|
86 |
+
""")
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
with gr.Column():
|
90 |
+
program_type = gr.Textbox(
|
91 |
+
label="Program Type",
|
92 |
+
placeholder="Describe the type of program (e.g., 'calculator', 'web scraper', 'data visualizer')",
|
93 |
+
lines=2
|
94 |
+
)
|
95 |
+
|
96 |
+
difficulty = gr.Radio(
|
97 |
+
choices=["Beginner", "Intermediate", "Advanced"],
|
98 |
+
label="Difficulty Level",
|
99 |
+
value="Intermediate",
|
100 |
+
info="Choose based on the complexity of the program"
|
101 |
+
)
|
102 |
+
|
103 |
+
features = gr.Radio(
|
104 |
+
choices=[
|
105 |
+
"Basic Functionality",
|
106 |
+
"Intermediate Features",
|
107 |
+
"Advanced Features"
|
108 |
+
],
|
109 |
+
label="Features",
|
110 |
+
value="Basic Functionality",
|
111 |
+
info="Select the level of features for your program"
|
112 |
+
)
|
113 |
+
|
114 |
+
documentation_level = gr.Radio(
|
115 |
+
choices=["Minimal", "Moderate", "Comprehensive"],
|
116 |
+
label="Documentation Level",
|
117 |
+
value="Moderate",
|
118 |
+
info="Choose the level of inline comments and documentation"
|
119 |
+
)
|
120 |
+
|
121 |
+
optimization_focus = gr.Radio(
|
122 |
+
choices=["Readability", "Performance", "Scalability"],
|
123 |
+
label="Optimization Focus",
|
124 |
+
value="Readability",
|
125 |
+
info="Specify the main focus for optimization"
|
126 |
+
)
|
127 |
+
|
128 |
+
output_format = gr.Radio(
|
129 |
+
choices=["Executable Code", "Code with Explanation", "Both"],
|
130 |
+
label="Output Format",
|
131 |
+
value="Both",
|
132 |
+
info="Choose how the output should be structured"
|
133 |
+
)
|
134 |
+
|
135 |
+
submit_btn = gr.Button(
|
136 |
+
"Generate Python Program",
|
137 |
+
variant="primary"
|
138 |
+
)
|
139 |
+
|
140 |
+
with gr.Column():
|
141 |
+
output = gr.Textbox(
|
142 |
+
label="Generated Python Program",
|
143 |
+
lines=20,
|
144 |
+
show_copy_button=True
|
145 |
+
)
|
146 |
+
|
147 |
+
# Example scenarios
|
148 |
+
gr.Examples(
|
149 |
+
examples=[
|
150 |
+
[
|
151 |
+
"Calculator for basic arithmetic",
|
152 |
+
"Beginner",
|
153 |
+
"Basic Functionality",
|
154 |
+
"Minimal",
|
155 |
+
"Readability",
|
156 |
+
"Executable Code"
|
157 |
+
],
|
158 |
+
[
|
159 |
+
"Web scraper for extracting data from websites",
|
160 |
+
"Intermediate",
|
161 |
+
"Intermediate Features",
|
162 |
+
"Moderate",
|
163 |
+
"Performance",
|
164 |
+
"Code with Explanation"
|
165 |
+
],
|
166 |
+
[
|
167 |
+
"Machine Learning model trainer",
|
168 |
+
"Advanced",
|
169 |
+
"Advanced Features",
|
170 |
+
"Comprehensive",
|
171 |
+
"Scalability",
|
172 |
+
"Both"
|
173 |
+
]
|
174 |
+
],
|
175 |
+
inputs=[
|
176 |
+
program_type,
|
177 |
+
difficulty,
|
178 |
+
features,
|
179 |
+
documentation_level,
|
180 |
+
optimization_focus,
|
181 |
+
output_format
|
182 |
+
],
|
183 |
+
outputs=output,
|
184 |
+
fn=generate_python_program,
|
185 |
+
cache_examples=True
|
186 |
+
)
|
187 |
+
|
188 |
+
# Usage tips
|
189 |
+
gr.Markdown("""
|
190 |
+
### π‘ Tips for Best Results
|
191 |
+
1. **Be Specific** with your program description - instead of "Tool", try "File Organizer".
|
192 |
+
2. **Match the Difficulty** to your programming experience.
|
193 |
+
3. **Experiment with Features** to customize your program.
|
194 |
+
4. **Choose the Right Focus** for optimization based on your needs.
|
195 |
+
|
196 |
+
### π― Documentation Options
|
197 |
+
- **Minimal**: Quick and simple understanding.
|
198 |
+
- **Moderate**: Key parts of the code are well-explained.
|
199 |
+
- **Comprehensive**: Detailed docstrings and examples included.
|
200 |
+
|
201 |
+
### π Remember
|
202 |
+
- Test your code after generation.
|
203 |
+
- Modify the generated code for better personalization.
|
204 |
+
- Use the examples to explore various possibilities.
|
205 |
+
""")
|
206 |
+
|
207 |
+
submit_btn.click(
|
208 |
+
fn=generate_python_program,
|
209 |
+
inputs=[
|
210 |
+
program_type,
|
211 |
+
difficulty,
|
212 |
+
features,
|
213 |
+
documentation_level,
|
214 |
+
optimization_focus,
|
215 |
+
output_format
|
216 |
+
],
|
217 |
+
outputs=output
|
218 |
+
)
|
219 |
+
|
220 |
+
return iface
|
221 |
+
|
222 |
+
if __name__ == "__main__":
|
223 |
+
iface = create_interface()
|
224 |
+
iface.launch()
|