Spaces:
Running
Running
Create app.py
Browse filesAdded app.py with secret variables.
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from groq import Groq
|
4 |
+
|
5 |
+
GROQ_API_KEY = os.environ.get("not_your_avg_key_mate")
|
6 |
+
client = Groq(api_key=GROQ_API_KEY)
|
7 |
+
|
8 |
+
PROMPT_ADVANCE = os.environ.get("prompt_code")
|
9 |
+
|
10 |
+
|
11 |
+
FOOTER_TEXT = """
|
12 |
+
<footer>
|
13 |
+
<p>If you enjoyed the functionality of the app, please leave a like!<br>
|
14 |
+
Check out more on <a href="https://www.linkedin.com/in/thisrudrapatel/" target="_blank">LinkedIn</a> |
|
15 |
+
<a href="https://www.thisrudrapatel.com/" target="_blank">Personal Website</a></p>
|
16 |
+
</footer>
|
17 |
+
"""
|
18 |
+
MAIN_PAGE_DISCLAIMER = """
|
19 |
+
<p style="font-style: italic; color:#000000; background-color: #d1ecf1; padding: 10px; border-radius: 5px;">
|
20 |
+
Disclaimer: This app is designed to generate human-readable explanations for Leetcode solutions.
|
21 |
+
Its primary purpose is to help you revisit your own logic later or understand unfamiliar (a.k.a. alien 👽) code in a clean, structured, and fun way.
|
22 |
+
Please review all outputs before sharing publicly, and treat the explanations as supportive drafts — not final answers.
|
23 |
+
</p>
|
24 |
+
"""
|
25 |
+
|
26 |
+
RAW_MARKDOWN_DISCLAIMER = """
|
27 |
+
<p style="font-style: italic; color:#000000; background-color: #d1ecf1; padding: 10px; border-radius: 5px;">
|
28 |
+
Heads-up: This is the raw markdown output generated for your Leetcode solution.
|
29 |
+
It's copy-paste ready for platforms like Leetcode Discuss, GitHub, or personal blogs.
|
30 |
+
Feel free to tweak the tone, structure, or formatting to better match your voice or audience.
|
31 |
+
</p>
|
32 |
+
"""
|
33 |
+
|
34 |
+
INSTRUCTIONS = """
|
35 |
+
<div style="background-color: #ccccccc; color: #ffffff; padding: 15px; border-radius: 8px; margin-bottom: 15px;">
|
36 |
+
<p><strong>Instructions & a Little Backstory 📖:</strong></p>
|
37 |
+
<p>
|
38 |
+
So here’s the deal — I built this tool just for fun (and for my own sanity). 😅
|
39 |
+
After solving a DSA problem, the last thing I wanted was to spend 15 more minutes formatting it for Leetcode Discuss. Clean markdown, proper explanation, structure — it adds up.
|
40 |
+
</p>
|
41 |
+
<p>
|
42 |
+
That’s where this app comes in clutch. I just drop my magic spell — a.k.a. my code — into the input box, click a button, and *BOOM*, it generates a beautiful, structured explanation with markdown. Upload-ready, dev-friendly, and cheeky as hell. Isn’t that fun?
|
43 |
+
</p>
|
44 |
+
<p>
|
45 |
+
By default, it follows a format I personally prompt-engineered 🧠 — one that includes sections like <strong>Intuition 💡</strong>, <strong>Approach 🪜</strong>, <strong>Code 👨🏽💻</strong>, and more — designed to make your solution stand out and feel human. But hey, you’re free to tweak it however you want! The output is pure markdown, so go wild if needed.
|
46 |
+
</p>
|
47 |
+
<p><strong>How to use:</strong></p>
|
48 |
+
<ul>
|
49 |
+
<li>Paste your Leetcode solution or the problem description into the text box.</li>
|
50 |
+
<li>Click <strong>“Get my Solution Fasttttt”</strong> to view the generated explanation.</li>
|
51 |
+
<li>Need the raw markdown? Switch to the <em>Raw Markdown Output 📝</em> tab to copy it directly.</li>
|
52 |
+
<li>Want an extra (or alternate) approach? Tap <em>“Suggest Alternate Solution 🔁”</em> and let it cook.</li>
|
53 |
+
</ul>
|
54 |
+
<p>
|
55 |
+
This app's just my little productivity sidekick — hope it becomes yours too.
|
56 |
+
If you vibe with it, feel free to drop a like, or connect on <a href="https://www.linkedin.com/in/thisrudrapatel/" target="_blank">LinkedIn</a>. Let’s grow together.
|
57 |
+
</p>
|
58 |
+
</div>
|
59 |
+
"""
|
60 |
+
|
61 |
+
TITLE = "<h1> Leetcode Solution Explainer </h1>"
|
62 |
+
|
63 |
+
def generate_response(message: str, system_prompt: str, temperature: float, max_tokens: int):
|
64 |
+
conversation = [
|
65 |
+
{"role": "system", "content": system_prompt},
|
66 |
+
{"role": "user", "content": message}
|
67 |
+
]
|
68 |
+
|
69 |
+
response = client.chat.completions.create(
|
70 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
71 |
+
messages=conversation,
|
72 |
+
temperature=temperature,
|
73 |
+
max_tokens=max_tokens,
|
74 |
+
stream=False
|
75 |
+
)
|
76 |
+
|
77 |
+
return response.choices[0].message.content
|
78 |
+
|
79 |
+
def analyze_solution(code, temperature, max_tokens):
|
80 |
+
prompt = f"""
|
81 |
+
{PROMPT_ADVANCE}
|
82 |
+
{code}
|
83 |
+
"""
|
84 |
+
markdown_output = generate_response(prompt, "You are an expert Leetcode Solution Writer.", temperature, max_tokens)
|
85 |
+
return markdown_output, markdown_output # Both rendered and raw
|
86 |
+
|
87 |
+
|
88 |
+
# UI Block
|
89 |
+
with gr.Blocks(theme="gradio/gsoft") as demo:
|
90 |
+
gr.HTML(TITLE)
|
91 |
+
|
92 |
+
with gr.Tab("Explain My Solution 🚀"):
|
93 |
+
gr.HTML(INSTRUCTIONS)
|
94 |
+
gr.HTML(MAIN_PAGE_DISCLAIMER)
|
95 |
+
code = gr.Textbox(label="Description", lines=5, placeholder="Paste your code or problem here...")
|
96 |
+
analyze_btn = gr.Button("Get my Solution Fasttttt")
|
97 |
+
with gr.Row():
|
98 |
+
output_rendered = gr.Markdown(label="Formatted Markdown") # Rendered
|
99 |
+
with gr.Accordion("Tweak Zone 🔧", open=True):
|
100 |
+
temperature = gr.Slider(0, 1, 0.1, value=0.5, label="Imagination Dial 🎨 (Temperature)")
|
101 |
+
max_tokens = gr.Slider(50, 1024, step=1, value=1024, label="How Much It’ll Say ✍️ (Tokens)")
|
102 |
+
|
103 |
+
with gr.Tab("Raw Markdown Output 📝"):
|
104 |
+
gr.HTML(RAW_MARKDOWN_DISCLAIMER)
|
105 |
+
raw_markdown_box = gr.Textbox(label="Raw Markdown", lines=20, show_copy_button=True)
|
106 |
+
|
107 |
+
analyze_btn.click(
|
108 |
+
analyze_solution,
|
109 |
+
inputs=[code, temperature, max_tokens],
|
110 |
+
outputs=[output_rendered, raw_markdown_box]
|
111 |
+
)
|
112 |
+
|
113 |
+
gr.HTML(FOOTER_TEXT)
|
114 |
+
|
115 |
+
if __name__ == "__main__":
|
116 |
+
demo.launch()
|