Sachin5112 commited on
Commit
f70a2f3
·
verified ·
1 Parent(s): 40502c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -1,12 +1,9 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load a tiny, CPU-friendly model for code explanation
5
- # 'codet5-small' is lightweight and handles multiple languages well
6
- # Change "summarization" to "text2text-generation"
7
- explainer = pipeline("text2text-generation", model="Salesforce/codet5-small")
8
 
9
- # Custom CSS to make it look "Zenith" style
10
  custom_css = """
11
  #container { max-width: 500px; margin: auto; }
12
  .gradio-container { background-color: #0b0f19; color: white; }
@@ -17,11 +14,19 @@ def explain_logic(code_input):
17
  if not code_input.strip():
18
  return "Please paste some code to get started!"
19
 
20
- # We prefix the input so the model knows it's an explanation task
21
- input_text = f"explain: {code_input}"
22
- result = explainer(input_text, max_length=100, min_length=30, do_sample=False)
23
- return result[0]['summary_text']
24
-
 
 
 
 
 
 
 
 
25
  # Building the Interface
26
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="cyan", secondary_hue="slate"), css=custom_css) as demo:
27
  gr.Markdown("## ⚡ Zenith Code Explainer")
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # We use "text-generation" which is confirmed available in your logs
5
+ explainer = pipeline("text-generation", model="Salesforce/codet5-small")
 
 
6
 
 
7
  custom_css = """
8
  #container { max-width: 500px; margin: auto; }
9
  .gradio-container { background-color: #0b0f19; color: white; }
 
14
  if not code_input.strip():
15
  return "Please paste some code to get started!"
16
 
17
+ # Prefix helps the model understand the context
18
+ input_text = f"Explain this code: {code_input}"
19
+
20
+ # We set max_new_tokens to control the length of the explanation
21
+ result = explainer(input_text, max_new_tokens=100, do_sample=False)
22
+
23
+ # text-generation returns a list with 'generated_text'
24
+ full_text = result[0]['generated_text']
25
+
26
+ # This removes the input prompt from the output so you only see the explanation
27
+ explanation = full_text.replace(input_text, "").strip()
28
+ return explanation if explanation else "The model couldn't generate an explanation for this snippet."
29
+
30
  # Building the Interface
31
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="cyan", secondary_hue="slate"), css=custom_css) as demo:
32
  gr.Markdown("## ⚡ Zenith Code Explainer")