Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
|
| 5 |
SYSTEM_PROMPT = """You are a professional coding teacher explaining code to a complete beginner.
|
| 6 |
|
|
@@ -264,20 +264,22 @@ def explain_code(code):
|
|
| 264 |
return "<p style='color:#8888aa; padding:1rem;'>⚠️ Please paste some code first!</p>"
|
| 265 |
|
| 266 |
try:
|
| 267 |
-
api_key = os.environ.get("
|
| 268 |
if not api_key:
|
| 269 |
-
return "<p style='color:#ff6a6a;padding:1rem;'>❌ Error:
|
| 270 |
|
| 271 |
-
client =
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
model="
|
| 275 |
max_tokens=2000,
|
| 276 |
-
|
| 277 |
-
|
|
|
|
|
|
|
| 278 |
)
|
| 279 |
-
|
| 280 |
-
result =
|
| 281 |
|
| 282 |
# Wrap in styled container
|
| 283 |
return f"""
|
|
@@ -342,7 +344,7 @@ with gr.Blocks(css=CSS, title="CodeLens — AI Code Explainer") as demo:
|
|
| 342 |
|
| 343 |
gr.HTML("""
|
| 344 |
<div style="text-align:center; padding:1.5rem 0 0.5rem; color:#8888aa; font-size:0.82rem; font-family:'DM Sans',sans-serif;">
|
| 345 |
-
Built with ❤️ using
|
| 346 |
</div>
|
| 347 |
""")
|
| 348 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
|
| 5 |
SYSTEM_PROMPT = """You are a professional coding teacher explaining code to a complete beginner.
|
| 6 |
|
|
|
|
| 264 |
return "<p style='color:#8888aa; padding:1rem;'>⚠️ Please paste some code first!</p>"
|
| 265 |
|
| 266 |
try:
|
| 267 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
| 268 |
if not api_key:
|
| 269 |
+
return "<p style='color:#ff6a6a;padding:1rem;'>❌ Error: GROQ_API_KEY secret is missing! Go to Space Settings → Variables and Secrets → Add GROQ_API_KEY</p>"
|
| 270 |
|
| 271 |
+
client = Groq(api_key=api_key)
|
| 272 |
+
|
| 273 |
+
response = client.chat.completions.create(
|
| 274 |
+
model="llama3-70b-8192",
|
| 275 |
max_tokens=2000,
|
| 276 |
+
messages=[
|
| 277 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 278 |
+
{"role": "user", "content": f"Explain this code:\n\n{code}"}
|
| 279 |
+
]
|
| 280 |
)
|
| 281 |
+
|
| 282 |
+
result = response.choices[0].message.content
|
| 283 |
|
| 284 |
# Wrap in styled container
|
| 285 |
return f"""
|
|
|
|
| 344 |
|
| 345 |
gr.HTML("""
|
| 346 |
<div style="text-align:center; padding:1.5rem 0 0.5rem; color:#8888aa; font-size:0.82rem; font-family:'DM Sans',sans-serif;">
|
| 347 |
+
Built with ❤️ using Groq AI · CodeLens for Beginners
|
| 348 |
</div>
|
| 349 |
""")
|
| 350 |
|