Henry Hickman commited on
Commit
5a3467a
·
1 Parent(s): 8fedf42

May need a gpu

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -1,16 +1,21 @@
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
  def update_language(level):
13
- # Map language names to Gradio code block identifiers
14
  lang_map = {
15
  "Python": "python",
16
  "C": "c",
@@ -19,7 +24,16 @@ def update_language(level):
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")
 
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",
 
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")