Spaces:
Sleeping
Sleeping
Henry Hickman commited on
Commit ·
ed47303
1
Parent(s): 0c96f74
New app
Browse files
app.py
CHANGED
|
@@ -1,7 +1,33 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Use a small model to start (you can swap later for stronger ones like Mixtral)
|
| 5 |
+
pipe = pipeline("text-generation", model="mistralai/Mixtral", max_new_tokens=200)
|
| 6 |
|
| 7 |
+
def explain_code(code, level):
|
| 8 |
+
prompt = f"Explain what the following {level.lower()} code does:\n\n{code}\n\nExplanation:"
|
| 9 |
+
result = pipe(prompt)[0]["generated_text"]
|
| 10 |
+
# Strip the prompt part off to clean the output
|
| 11 |
+
explanation = result[len(prompt):].strip()
|
| 12 |
+
return explanation
|
| 13 |
+
|
| 14 |
+
iface = gr.Interface(
|
| 15 |
+
fn=explain_code,
|
| 16 |
+
inputs=[
|
| 17 |
+
gr.Textbox(lines=12, label="Paste your code here"),
|
| 18 |
+
gr.Radio(["Python", "C", "JavaScript", "Other"], label="Language", value="Python")
|
| 19 |
+
],
|
| 20 |
+
outputs=gr.Textbox(label="Explanation", lines=8),
|
| 21 |
+
title="💡 Code Explainer",
|
| 22 |
+
description=(
|
| 23 |
+
"Enter code and get a natural language explanation. "
|
| 24 |
+
"Powered by a Hugging Face text-generation model."
|
| 25 |
+
),
|
| 26 |
+
theme="soft",
|
| 27 |
+
examples=[
|
| 28 |
+
["for i in range(5): print(i*i)", "Python"],
|
| 29 |
+
["for(int i=0;i<5;i++){System.out.println(i*i);}", "C"],
|
| 30 |
+
]
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
iface.launch()
|