WaveOAK commited on
Commit
5caf9d8
·
verified ·
1 Parent(s): 745a1cd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import requests
4
+
5
+ def convert_to_cpp(python_code):
6
+ # This grabs the key from the secret safe
7
+ api_key = os.getenv("GROQ_API_KEY")
8
+
9
+ if not api_key:
10
+ return "// Error: API Key is missing. Check Settings!"
11
+
12
+ if not python_code.strip():
13
+ return "// Error: Please type some Python code."
14
+
15
+ # Talking to the Smart AI
16
+ url = "https://api.groq.com/openai/v1/chat/completions"
17
+ headers = {
18
+ "Authorization": f"Bearer {api_key}",
19
+ "Content-Type": "application/json"
20
+ }
21
+
22
+ # Telling the AI to be a C++ Expert
23
+ system_prompt = """You are an expert C++20 developer.
24
+ Convert this Python code to efficient C++.
25
+ Add comments. Do NOT use markdown ticks."""
26
+
27
+ data = {
28
+ "model": "llama3-8b-8192",
29
+ "messages": [
30
+ {"role": "system", "content": system_prompt},
31
+ {"role": "user", "content": python_code}
32
+ ],
33
+ "temperature": 0.2
34
+ }
35
+
36
+ try:
37
+ response = requests.post(url, json=data, headers=headers)
38
+ if response.status_code != 200:
39
+ return f"// API Error: {response.text}"
40
+ return response.json()['choices'][0]['message']['content']
41
+ except Exception as e:
42
+ return f"// Connection Error: {str(e)}"
43
+
44
+ # The Visual Part (The Screen)
45
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
46
+ gr.Markdown("# 🐍 Python to ⚡ C++ Converter")
47
+ gr.Markdown("Transform slow Python scripts into fast C++ code.")
48
+
49
+ with gr.Row():
50
+ with gr.Column():
51
+ py_input = gr.Code(language="python", label="Paste Python Here", lines=15)
52
+ convert_btn = gr.Button("Convert 🚀", variant="primary")
53
+
54
+ with gr.Column():
55
+ cpp_output = gr.Code(language="cpp", label="C++ Result", lines=15, interactive=False)
56
+
57
+ convert_btn.click(fn=convert_to_cpp, inputs=py_input, outputs=cpp_output)
58
+
59
+ if __name__ == "__main__":
60
+ app.launch()