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