TheHickman commited on
Commit
8281054
·
verified ·
1 Parent(s): 683cc29

More debugging

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
 
5
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
6
 
7
- # ---- GPT-5 explanation backend ----
8
  def explain_text(selected_text):
9
  if selected_text is None:
10
  return ""
@@ -12,21 +12,19 @@ def explain_text(selected_text):
12
  if not selected_text:
13
  return "Please select or enter some text first."
14
 
15
- prompt = f"""
16
- You are an expert machine learning instructor. The user highlighted the following text from a learning resource:
17
-
18
- \"\"\"
19
- {selected_text}
20
- \"\"\"
21
-
22
- Explain it clearly and intuitively. Assume the reader has basic ML knowledge. Keep it concise and educational.
23
- """
24
-
25
- response = client.responses.create(
26
- model="gpt-5",
27
- input=prompt,
28
- )
29
- return response.output_text
30
 
31
  # ---- Page content ----
32
  PAGE_HTML = """
@@ -52,24 +50,20 @@ with gr.Blocks(head="""
52
  document.addEventListener("mouseup", () => {
53
  const selection = window.getSelection().toString().trim();
54
  if (selection.length > 0) {
55
- // Find the Gradio textbox by its data attribute
56
  const textbox = document.querySelector('textarea[data-testid="textbox"]');
57
  if (textbox) {
58
- // Update the value
59
  const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
60
  window.HTMLTextAreaElement.prototype,
61
  "value"
62
  ).set;
63
  nativeInputValueSetter.call(textbox, selection);
64
-
65
- // Trigger change event so Gradio picks it up
66
  textbox.dispatchEvent(new Event("input", { bubbles: true }));
67
  }
68
  }
69
  });
70
  </script>
71
  """) as demo:
72
- gr.Markdown("### 📘 Highlight text and ask GPT-5 for help")
73
  gr.HTML(PAGE_HTML)
74
 
75
  selected_text = gr.Textbox(
 
4
 
5
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
6
 
7
+ # ---- GPT explanation backend ----
8
  def explain_text(selected_text):
9
  if selected_text is None:
10
  return ""
 
12
  if not selected_text:
13
  return "Please select or enter some text first."
14
 
15
+ try:
16
+ response = client.chat.completions.create(
17
+ model="gpt-4o", # or "gpt-4-turbo" or "gpt-3.5-turbo"
18
+ messages=[
19
+ {"role": "system", "content": "You are an expert machine learning instructor. Explain concepts clearly and intuitively for learners with basic ML knowledge. Keep explanations concise and educational."},
20
+ {"role": "user", "content": f"Explain this text from a learning resource:\n\n\"\"\"\n{selected_text}\n\"\"\""}
21
+ ],
22
+ temperature=0.7,
23
+ max_tokens=500
24
+ )
25
+ return response.choices[0].message.content
26
+ except Exception as e:
27
+ return f"Error: {str(e)}"
 
 
28
 
29
  # ---- Page content ----
30
  PAGE_HTML = """
 
50
  document.addEventListener("mouseup", () => {
51
  const selection = window.getSelection().toString().trim();
52
  if (selection.length > 0) {
 
53
  const textbox = document.querySelector('textarea[data-testid="textbox"]');
54
  if (textbox) {
 
55
  const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
56
  window.HTMLTextAreaElement.prototype,
57
  "value"
58
  ).set;
59
  nativeInputValueSetter.call(textbox, selection);
 
 
60
  textbox.dispatchEvent(new Event("input", { bubbles: true }));
61
  }
62
  }
63
  });
64
  </script>
65
  """) as demo:
66
+ gr.Markdown("### 📘 Highlight text and ask GPT for help")
67
  gr.HTML(PAGE_HTML)
68
 
69
  selected_text = gr.Textbox(