WaveOAK's picture
Update app.py
b2ec955 verified
import gradio as gr
import os
import requests
def convert_to_cpp(python_code):
# 1. Get the API Key securely
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
return "// Error: API Key is missing. Please check your Settings!"
if not python_code.strip():
return "// Error: Please enter some Python code first."
# 2. Setup the "Senior Developer" Persona
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# We instruct the AI to ONLY return code, no chatting.
system_prompt = """You are an expert C++20 developer.
Your task is to convert Python code into highly efficient, modern C++.
- Use standard libraries (std::vector, std::string) where possible.
- Include necessary headers (#include <iostream>, etc).
- Add brief comments explaining complex translations.
- Do NOT output markdown ticks (```cpp). Just output the raw code.
"""
data = {
# --- THE FIX IS HERE: UPDATED MODEL NAME ---
"model": "llama-3.1-8b-instant",
# -------------------------------------------
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": python_code}
],
"temperature": 0.2
}
try:
response = requests.post(url, json=data, headers=headers)
if response.status_code != 200:
return f"// API Error: {response.text}"
return response.json()['choices'][0]['message']['content']
except Exception as e:
return f"// Connection Error: {str(e)}"
# 3. Build the Professional UI
with gr.Blocks(theme=gr.themes.Soft()) as app:
gr.Markdown("# 🐍 Python to ⚑ C++ Converter")
gr.Markdown("Transform slow Python scripts into high-performance C++ code using Llama 3.1.")
with gr.Row():
# Left side: Python Input
with gr.Column():
py_input = gr.Code(
language="python",
label="Paste Python Code Here",
lines=15
)
convert_btn = gr.Button("Convert to C++ πŸš€", variant="primary")
# Right side: C++ Output
with gr.Column():
cpp_output = gr.Code(
language="cpp",
label="C++ Result",
lines=15,
interactive=False # User can copy but not edit
)
# Connect the button
convert_btn.click(fn=convert_to_cpp, inputs=py_input, outputs=cpp_output)
if __name__ == "__main__":
app.launch()