Henry Hickman commited on
Commit
8fedf42
·
1 Parent(s): 0a8b56b

GPT-2 is back:

Browse files
Files changed (1) hide show
  1. app.py +23 -17
app.py CHANGED
@@ -9,20 +9,26 @@ def explain_code(code, level):
9
  explanation = result[len(prompt):].strip()
10
  return explanation
11
 
12
- iface = gr.Interface(
13
- fn=explain_code,
14
- inputs=[
15
- gr.Textbox(lines=12, label="Paste your code here"),
16
- gr.Radio(["Python", "C", "JavaScript", "Other"], label="Language", value="Python")
17
- ],
18
- outputs=gr.Textbox(label="Explanation", lines=8),
19
- title="💡 Code Explainer",
20
- description="Enter code and get a natural language explanation.",
21
- theme="soft",
22
- examples=[
23
- ["for i in range(5): print(i*i)", "Python"],
24
- ["for(int i=0;i<5;i++){System.out.println(i*i);}", "C"],
25
- ]
26
- )
27
-
28
- iface.launch()
 
 
 
 
 
 
 
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",
17
+ "JavaScript": "javascript",
18
+ "Other": "text"
19
+ }
20
+ return gr.update(language=lang_map.get(level, "text"))
21
+
22
+ with gr.Blocks(title="💡 Code Explainer") as demo:
23
+ gr.Markdown("### Enter code and get a natural language explanation.")
24
+
25
+ lang = gr.Radio(["Python", "C", "JavaScript", "Other"], value="Python", label="Language")
26
+ code = gr.Code(language="python", label="Code", lines=12)
27
+ output = gr.Textbox(label="Explanation", lines=8)
28
+
29
+ lang.change(fn=update_language, inputs=lang, outputs=code)
30
+ btn = gr.Button("Explain Code")
31
+
32
+ btn.click(fn=explain_code, inputs=[code, lang], outputs=output)
33
+
34
+ demo.launch()