Spaces:
Sleeping
Sleeping
Adding debugging
Browse files
app.py
CHANGED
|
@@ -8,87 +8,84 @@ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
| 8 |
def explain_text(selected_text):
|
| 9 |
if selected_text is None:
|
| 10 |
return ""
|
| 11 |
-
|
| 12 |
selected_text = selected_text.strip()
|
| 13 |
if not selected_text:
|
| 14 |
return "Please select or enter some text first."
|
| 15 |
-
|
| 16 |
prompt = f"""
|
| 17 |
-
You are an expert machine learning instructor.
|
| 18 |
-
|
| 19 |
-
The user highlighted the following text from a learning resource:
|
| 20 |
|
| 21 |
\"\"\"
|
| 22 |
{selected_text}
|
| 23 |
\"\"\"
|
| 24 |
|
| 25 |
-
Explain it clearly and intuitively.
|
| 26 |
-
Assume the reader has basic ML knowledge.
|
| 27 |
-
Keep it concise and educational.
|
| 28 |
"""
|
| 29 |
-
|
| 30 |
response = client.responses.create(
|
| 31 |
model="gpt-5",
|
| 32 |
input=prompt,
|
| 33 |
)
|
| 34 |
-
|
| 35 |
return response.output_text
|
| 36 |
|
| 37 |
-
|
| 38 |
-
# ---- Page content (replace later with markdown file) ----
|
| 39 |
PAGE_HTML = """
|
| 40 |
<div id="content" style="max-width: 800px; margin: auto; font-size: 16px; line-height: 1.6;">
|
| 41 |
-
<h1>Text Generation</h1>
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
<
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
<p>
|
| 54 |
-
During inference, the model repeatedly samples the most likely next token
|
| 55 |
-
until a stopping condition is reached.
|
| 56 |
-
</p>
|
| 57 |
</div>
|
|
|
|
| 58 |
|
|
|
|
| 59 |
<script>
|
| 60 |
document.addEventListener("mouseup", () => {
|
| 61 |
-
const selection = window.getSelection().toString();
|
| 62 |
if (selection.length > 0) {
|
| 63 |
-
|
|
|
|
| 64 |
if (textbox) {
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
textbox.dispatchEvent(new Event("input", { bubbles: true }));
|
| 67 |
}
|
| 68 |
}
|
| 69 |
});
|
| 70 |
</script>
|
| 71 |
-
"""
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
with gr.Blocks() as demo:
|
| 75 |
gr.Markdown("### 📘 Highlight text and ask GPT-5 for help")
|
| 76 |
-
|
| 77 |
gr.HTML(PAGE_HTML)
|
| 78 |
-
|
| 79 |
selected_text = gr.Textbox(
|
| 80 |
label="Selected text",
|
| 81 |
placeholder="Highlight text above...",
|
| 82 |
-
|
|
|
|
| 83 |
)
|
| 84 |
-
|
| 85 |
explain_btn = gr.Button("Explain selection 🧠")
|
| 86 |
output = gr.Markdown()
|
| 87 |
-
|
| 88 |
explain_btn.click(
|
| 89 |
fn=explain_text,
|
| 90 |
inputs=selected_text,
|
| 91 |
outputs=output,
|
| 92 |
)
|
| 93 |
|
| 94 |
-
demo.launch()
|
|
|
|
| 8 |
def explain_text(selected_text):
|
| 9 |
if selected_text is None:
|
| 10 |
return ""
|
|
|
|
| 11 |
selected_text = selected_text.strip()
|
| 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 = """
|
| 33 |
<div id="content" style="max-width: 800px; margin: auto; font-size: 16px; line-height: 1.6;">
|
| 34 |
+
<h1>Text Generation</h1>
|
| 35 |
+
<p>
|
| 36 |
+
Text generation is the task of producing natural language text given an input prompt.
|
| 37 |
+
It is commonly used for chatbots, creative writing, summarization, and code generation.
|
| 38 |
+
</p>
|
| 39 |
+
<p>
|
| 40 |
+
Most modern text generation models are based on the transformer architecture and are
|
| 41 |
+
trained using next-token prediction.
|
| 42 |
+
</p>
|
| 43 |
+
<p>
|
| 44 |
+
During inference, the model repeatedly samples the most likely next token until a
|
| 45 |
+
stopping condition is reached.
|
| 46 |
+
</p>
|
|
|
|
|
|
|
|
|
|
| 47 |
</div>
|
| 48 |
+
"""
|
| 49 |
|
| 50 |
+
with gr.Blocks(head="""
|
| 51 |
<script>
|
| 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(
|
| 76 |
label="Selected text",
|
| 77 |
placeholder="Highlight text above...",
|
| 78 |
+
lines=3,
|
| 79 |
+
interactive=True
|
| 80 |
)
|
| 81 |
+
|
| 82 |
explain_btn = gr.Button("Explain selection 🧠")
|
| 83 |
output = gr.Markdown()
|
| 84 |
+
|
| 85 |
explain_btn.click(
|
| 86 |
fn=explain_text,
|
| 87 |
inputs=selected_text,
|
| 88 |
outputs=output,
|
| 89 |
)
|
| 90 |
|
| 91 |
+
demo.launch()
|