Spaces:
Sleeping
Sleeping
Henry Hickman commited on
Commit ·
df5eb2f
1
Parent(s): 016a960
Updated model to be better
Browse files
app.py
CHANGED
|
@@ -1,16 +1,22 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def explain_code(code, level):
|
| 7 |
-
prompt = f"Explain what the following {level.lower()} code does:\n\n{code}\n\nExplanation:"
|
| 8 |
result = pipe(prompt)[0]["generated_text"]
|
| 9 |
explanation = result[len(prompt):].strip()
|
| 10 |
return explanation
|
| 11 |
|
| 12 |
def update_language(level):
|
| 13 |
-
# Map language names to Gradio code block identifiers
|
| 14 |
lang_map = {
|
| 15 |
"Python": "python",
|
| 16 |
"C": "c",
|
|
@@ -19,7 +25,16 @@ def update_language(level):
|
|
| 19 |
}
|
| 20 |
return gr.update(language=lang_map.get(level, "text"))
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
gr.Markdown("### Enter code and get a natural language explanation.")
|
| 24 |
|
| 25 |
lang = gr.Radio(["Python", "C", "JavaScript", "Other"], value="Python", label="Language")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Use a model tuned for reasoning and instruction following
|
| 5 |
+
pipe = pipeline(
|
| 6 |
+
"text-generation",
|
| 7 |
+
model="microsoft/Phi-3-mini-4k-instruct",
|
| 8 |
+
torch_dtype="auto",
|
| 9 |
+
device_map="auto",
|
| 10 |
+
max_new_tokens=300
|
| 11 |
+
)
|
| 12 |
|
| 13 |
def explain_code(code, level):
|
| 14 |
+
prompt = f"Explain clearly what the following {level.lower()} code does:\n\n{code}\n\nExplanation:"
|
| 15 |
result = pipe(prompt)[0]["generated_text"]
|
| 16 |
explanation = result[len(prompt):].strip()
|
| 17 |
return explanation
|
| 18 |
|
| 19 |
def update_language(level):
|
|
|
|
| 20 |
lang_map = {
|
| 21 |
"Python": "python",
|
| 22 |
"C": "c",
|
|
|
|
| 25 |
}
|
| 26 |
return gr.update(language=lang_map.get(level, "text"))
|
| 27 |
|
| 28 |
+
custom_css = """
|
| 29 |
+
div.svelte-1ipelgc, div.ace_content, .ace_editor {
|
| 30 |
+
cursor: text !important;
|
| 31 |
+
}
|
| 32 |
+
.ace_editor {
|
| 33 |
+
pointer-events: auto !important;
|
| 34 |
+
}
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
with gr.Blocks(css=custom_css, title="💡 Code Explainer") as demo:
|
| 38 |
gr.Markdown("### Enter code and get a natural language explanation.")
|
| 39 |
|
| 40 |
lang = gr.Radio(["Python", "C", "JavaScript", "Other"], value="Python", label="Language")
|