-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
97 lines (71 loc) · 2.1 KB
/
app.py
File metadata and controls
97 lines (71 loc) · 2.1 KB
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
from flask import Flask, render_template, request
from groq import Groq
from dotenv import load_dotenv
import os
import re
import html
load_dotenv()
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
app = Flask(__name__)
def format_output(text):
# Escape HTML
text = html.escape(text)
# Headings
text = text.replace("### Language Detected", "<h3>Language Detected</h3>")
text = text.replace("### Code Explanation", "<h3>Code Explanation</h3>")
text = text.replace("### Errors Found", "<h3>Errors Found</h3>")
text = text.replace("### Improvements", "<h3>Improvements</h3>")
text = text.replace("### Time & Space Complexity", "<h3>⏱ Time & Space Complexity</h3>")
text = text.replace("### Code Quality Score", "<h3>Code Quality Score</h3>")
text = text.replace("### Improved Code", "<h3>Improved Code</h3>")
# Code blocks
text = re.sub(
r"```(.*?)```",
r"""
<div class="code-box">
<button class="copy-btn" onclick="copyCode()">Copy</button>
<pre><code>\1</code></pre>
</div>
""",
text,
flags=re.DOTALL
)
text = text.replace("\n", "<br>")
return text
@app.route("/", methods=["GET", "POST"])
def index():
result = ""
code = ""
if request.method == "POST":
code = request.form.get("code")
prompt = f"""
You are an AI Code Analyzer.
Return the response strictly in this format:
### Language Detected
<language>
### Code Explanation
<explanation>
### Errors Found
<errors or none>
### Improvements
<suggestions>
### Time & Space Complexity
Time:
Space:
### Code Quality Score
<score>/10
### Improved Code
```python
<only code>
Analyze this code:
{code}
"""
response = client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[{"role": "user", "content": prompt}],
temperature=0.3
)
result = format_output(response.choices[0].message.content)
return render_template("index.html", result=result, code=code)
if __name__ == "__main__":
app.run(debug=True, use_reloader=False)