TheHickman commited on
Commit
6be3f7b
·
verified ·
1 Parent(s): b92e0fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -61
app.py CHANGED
@@ -32,7 +32,7 @@ def explain_text(selected_text, current_html):
32
  },
33
  {
34
  "role": "user",
35
- "content": f"Explain this text from a learning resource in 2-3 clear, simple sentences for a beginner:\n\n\"{selected_text}\""
36
  }
37
  ],
38
  max_tokens=300,
@@ -45,27 +45,26 @@ def explain_text(selected_text, current_html):
45
  return current_html, f"❌ Error: {str(e)}"
46
 
47
  replacement = (
48
- f'<mark class="explained-original" title="Original text">{selected_text}</mark>'
49
  f'<span class="explanation-block">💡 {explanation}</span>'
50
  )
51
 
52
  new_html = current_html.replace(selected_text, replacement, 1)
53
 
54
  if new_html == current_html:
55
- return current_html, "⚠️ Could not find selected text in the document. Try selecting again."
56
 
57
- return new_html, f"✅ Explained: \"{selected_text[:60]}{'...' if len(selected_text) > 60 else ''}\""
58
 
59
 
60
  def reset_content():
61
- return INITIAL_HTML, "", "Document reset."
62
 
63
 
64
  CSS = """
65
- @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@600&family=Source+Serif+4:ital,opsz,wght@0,8..60,400;1,8..60,300&display=swap');
66
  body { background: #faf8f4 !important; }
67
  #reader-content {
68
- font-family: 'Source Serif 4', Georgia, serif;
69
  font-size: 1.05rem;
70
  line-height: 1.85;
71
  color: #2c2010;
@@ -77,25 +76,16 @@ body { background: #faf8f4 !important; }
77
  }
78
  .content-para { margin-bottom: 1.4em; }
79
  mark.explained-original {
80
- background: linear-gradient(120deg, #f5e6a3, #f0d870);
81
- border-radius: 3px;
82
- padding: 1px 3px;
83
  text-decoration: line-through;
84
- text-decoration-color: #c8a800;
85
- color: #6b5a10;
86
- font-style: italic;
87
- opacity: 0.75;
88
  }
89
  .explanation-block {
90
  display: inline-block;
91
  background: #fffbee;
92
  border-left: 3px solid #d4a800;
93
- border-radius: 0 6px 6px 0;
94
  padding: 6px 12px;
95
  margin: 4px 2px;
96
- color: #3a2e08;
97
- font-size: 0.93rem;
98
- line-height: 1.6;
99
  }
100
  """
101
 
@@ -118,60 +108,57 @@ function setupSelectionCapture() {
118
  setupSelectionCapture();
119
  """
120
 
121
- with gr.Blocks(css=CSS, js=JS_CAPTURE_SELECTION, title="ML Reader — Highlight & Explain") as demo:
122
 
123
  gr.HTML("""
124
- <div style="max-width: 720px; margin: 0 auto; padding: 32px 16px 0;">
125
- <h1 style="font-family:'Playfair Display',Georgia,serif; font-size:2.1rem; color:#1a1208; margin-bottom:4px;">
126
- Text Generation
127
- </h1>
128
- <p style="font-family:'Source Serif 4',Georgia,serif; font-size:0.82rem; color:#a09070; letter-spacing:0.08em; text-transform:uppercase; margin:0 0 12px;">
129
- Machine Learning · Reading Guide
130
  </p>
131
  </div>
132
  """)
133
 
134
- with gr.Column(elem_style="max-width:720px; margin:0 auto; padding:0 16px 60px;"):
 
135
 
136
- content_html = gr.HTML(
137
- value=f'<div id="reader-content">{INITIAL_HTML}</div>',
138
- label="",
139
- )
140
 
141
- selected_text = gr.Textbox(
142
- label="Selected text",
143
- placeholder="Highlight text above — it will appear here automatically…",
144
- lines=2,
145
- elem_classes=["selected-box"],
146
- show_label=True,
147
- )
148
 
149
- with gr.Row():
150
- explain_btn = gr.Button("✦ Explain Selection", variant="primary")
151
- reset_btn = gr.Button("↺ Reset")
152
 
153
- status = gr.Textbox(label="", interactive=False, show_label=False)
154
 
155
- html_state = gr.State(f'<div id="reader-content">{INITIAL_HTML}</div>')
156
 
157
- explain_btn.click(
158
- fn=explain_text,
159
- inputs=[selected_text, html_state],
160
- outputs=[html_state, status],
161
- ).then(
162
- fn=lambda h: h,
163
- inputs=[html_state],
164
- outputs=[content_html],
165
- )
166
 
167
- reset_btn.click(
168
- fn=reset_content,
169
- inputs=[],
170
- outputs=[html_state, selected_text, status],
171
- ).then(
172
- fn=lambda h: f'<div id="reader-content">{INITIAL_HTML}</div>',
173
- inputs=[html_state],
174
- outputs=[content_html],
175
- )
 
176
 
177
- demo.launch()
 
32
  },
33
  {
34
  "role": "user",
35
+ "content": f"Explain this text in 2-3 clear, simple sentences:\n\n\"{selected_text}\""
36
  }
37
  ],
38
  max_tokens=300,
 
45
  return current_html, f"❌ Error: {str(e)}"
46
 
47
  replacement = (
48
+ f'<mark class="explained-original">{selected_text}</mark>'
49
  f'<span class="explanation-block">💡 {explanation}</span>'
50
  )
51
 
52
  new_html = current_html.replace(selected_text, replacement, 1)
53
 
54
  if new_html == current_html:
55
+ return current_html, "⚠️ Could not find selected text in the document."
56
 
57
+ return new_html, "✅ Explanation added."
58
 
59
 
60
  def reset_content():
61
+ return f'<div id="reader-content">{INITIAL_HTML}</div>', "", "Document reset."
62
 
63
 
64
  CSS = """
 
65
  body { background: #faf8f4 !important; }
66
  #reader-content {
67
+ font-family: Georgia, serif;
68
  font-size: 1.05rem;
69
  line-height: 1.85;
70
  color: #2c2010;
 
76
  }
77
  .content-para { margin-bottom: 1.4em; }
78
  mark.explained-original {
79
+ background: #f5e6a3;
 
 
80
  text-decoration: line-through;
81
+ opacity: 0.7;
 
 
 
82
  }
83
  .explanation-block {
84
  display: inline-block;
85
  background: #fffbee;
86
  border-left: 3px solid #d4a800;
 
87
  padding: 6px 12px;
88
  margin: 4px 2px;
 
 
 
89
  }
90
  """
91
 
 
108
  setupSelectionCapture();
109
  """
110
 
111
+ with gr.Blocks(title="ML Reader — Highlight & Explain") as demo:
112
 
113
  gr.HTML("""
114
+ <div style="max-width:720px; margin:0 auto; padding:32px 16px 0;">
115
+ <h1>Text Generation</h1>
116
+ <p style="color:#888; font-size:0.9rem;">
117
+ Highlight text below and click Explain.
 
 
118
  </p>
119
  </div>
120
  """)
121
 
122
+ # Wrap content in a styled HTML container instead of elem_style
123
+ gr.HTML('<div style="max-width:720px; margin:0 auto; padding:0 16px 60px;">')
124
 
125
+ content_html = gr.HTML(
126
+ value=f'<div id="reader-content">{INITIAL_HTML}</div>'
127
+ )
 
128
 
129
+ selected_text = gr.Textbox(
130
+ label="Selected text",
131
+ lines=2,
132
+ elem_classes=["selected-box"],
133
+ )
 
 
134
 
135
+ with gr.Row():
136
+ explain_btn = gr.Button("✦ Explain Selection")
137
+ reset_btn = gr.Button("↺ Reset")
138
 
139
+ status = gr.Textbox(label="", interactive=False)
140
 
141
+ html_state = gr.State(f'<div id="reader-content">{INITIAL_HTML}</div>')
142
 
143
+ explain_btn.click(
144
+ fn=explain_text,
145
+ inputs=[selected_text, html_state],
146
+ outputs=[html_state, status],
147
+ ).then(
148
+ lambda h: h,
149
+ inputs=[html_state],
150
+ outputs=[content_html],
151
+ )
152
 
153
+ reset_btn.click(
154
+ fn=reset_content,
155
+ outputs=[html_state, selected_text, status],
156
+ ).then(
157
+ lambda h: h,
158
+ inputs=[html_state],
159
+ outputs=[content_html],
160
+ )
161
+
162
+ gr.HTML('</div>')
163
 
164
+ demo.launch(css=CSS, js=JS_CAPTURE_SELECTION)