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

Reverting to gpt-2 until I get a GPU

Browse files
Files changed (1) hide show
  1. app.py +18 -38
app.py CHANGED
@@ -1,48 +1,28 @@
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
- device=-1, # <-- forces CPU
9
- max_new_tokens=300
10
- )
11
 
12
  def explain_code(code, level):
13
- prompt = f"Explain clearly what the following {level.lower()} code does:\n\n{code}\n\nExplanation:"
14
  result = pipe(prompt)[0]["generated_text"]
15
  explanation = result[len(prompt):].strip()
16
  return explanation
17
 
18
- def update_language(level):
19
- lang_map = {
20
- "Python": "python",
21
- "C": "c",
22
- "JavaScript": "javascript",
23
- "Other": "text"
24
- }
25
- return gr.update(language=lang_map.get(level, "text"))
26
-
27
- custom_css = """
28
- div.svelte-1ipelgc, div.ace_content, .ace_editor {
29
- cursor: text !important;
30
- }
31
- .ace_editor {
32
- pointer-events: auto !important;
33
- }
34
- """
35
-
36
- with gr.Blocks(css=custom_css, title="💡 Code Explainer") as demo:
37
- gr.Markdown("### Enter code and get a natural language explanation.")
38
-
39
- lang = gr.Radio(["Python", "C", "JavaScript", "Other"], value="Python", label="Language")
40
- code = gr.Code(language="python", label="Code", lines=12)
41
- output = gr.Textbox(label="Explanation", lines=8)
42
-
43
- lang.change(fn=update_language, inputs=lang, outputs=code)
44
- btn = gr.Button("Explain Code")
45
-
46
- btn.click(fn=explain_code, inputs=[code, lang], outputs=output)
47
 
48
- demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ pipe = pipeline("text-generation", model="gpt2", max_new_tokens=200)
 
 
 
 
 
 
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
+ 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()