Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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"
|
| 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,
|
| 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:
|
| 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:
|
| 81 |
-
border-radius: 3px;
|
| 82 |
-
padding: 1px 3px;
|
| 83 |
text-decoration: line-through;
|
| 84 |
-
|
| 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(
|
| 122 |
|
| 123 |
gr.HTML("""
|
| 124 |
-
<div style="max-width:
|
| 125 |
-
<h1
|
| 126 |
-
|
| 127 |
-
|
| 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 |
-
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
show_label=True,
|
| 147 |
-
)
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
|
| 153 |
-
|
| 154 |
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 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)
|