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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -14
app.py CHANGED
@@ -3,57 +3,78 @@ 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__":
 
3
  import requests
4
 
5
  def convert_to_cpp(python_code):
6
+ # 1. Get the API Key securely
7
  api_key = os.getenv("GROQ_API_KEY")
8
 
9
  if not api_key:
10
+ return "// Error: API Key is missing. Please check your Settings!"
11
 
12
  if not python_code.strip():
13
+ return "// Error: Please enter some Python code first."
14
 
15
+ # 2. Setup the "Senior Developer" Persona
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
+ # We instruct the AI to ONLY return code, no chatting.
23
  system_prompt = """You are an expert C++20 developer.
24
+ Your task is to convert Python code into highly efficient, modern C++.
25
+ - Use standard libraries (std::vector, std::string) where possible.
26
+ - Include necessary headers (#include <iostream>, etc).
27
+ - Add brief comments explaining complex translations.
28
+ - Do NOT output markdown ticks (```cpp). Just output the raw code.
29
+ """
30
 
31
  data = {
32
+ # --- THE FIX IS HERE: UPDATED MODEL NAME ---
33
+ "model": "llama-3.1-8b-instant",
34
+ # -------------------------------------------
35
  "messages": [
36
  {"role": "system", "content": system_prompt},
37
  {"role": "user", "content": python_code}
38
  ],
39
+ "temperature": 0.2
40
  }
41
 
42
  try:
43
  response = requests.post(url, json=data, headers=headers)
44
+
45
  if response.status_code != 200:
46
  return f"// API Error: {response.text}"
47
+
48
  return response.json()['choices'][0]['message']['content']
49
+
50
  except Exception as e:
51
  return f"// Connection Error: {str(e)}"
52
 
53
+ # 3. Build the Professional UI
54
  with gr.Blocks(theme=gr.themes.Soft()) as app:
55
  gr.Markdown("# 🐍 Python to ⚡ C++ Converter")
56
+ gr.Markdown("Transform slow Python scripts into high-performance C++ code using Llama 3.1.")
57
 
58
  with gr.Row():
59
+ # Left side: Python Input
60
  with gr.Column():
61
+ py_input = gr.Code(
62
+ language="python",
63
+ label="Paste Python Code Here",
64
+ lines=15
65
+ )
66
+ convert_btn = gr.Button("Convert to C++ 🚀", variant="primary")
67
 
68
+ # Right side: C++ Output
69
  with gr.Column():
70
+ cpp_output = gr.Code(
71
+ language="cpp",
72
+ label="C++ Result",
73
+ lines=15,
74
+ interactive=False # User can copy but not edit
75
+ )
76
 
77
+ # Connect the button
78
  convert_btn.click(fn=convert_to_cpp, inputs=py_input, outputs=cpp_output)
79
 
80
  if __name__ == "__main__":