Spaces:
Sleeping
Sleeping
Toggle for A/B testing
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ def explain_text(selected_text):
|
|
| 14 |
|
| 15 |
try:
|
| 16 |
response = client.chat.completions.create(
|
| 17 |
-
model="gpt-4o",
|
| 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\"\"\""}
|
|
@@ -26,8 +26,8 @@ def explain_text(selected_text):
|
|
| 26 |
except Exception as e:
|
| 27 |
return f"Error: {str(e)}"
|
| 28 |
|
| 29 |
-
# ----
|
| 30 |
-
|
| 31 |
<div id="content" style="max-width: 800px; margin: auto; font-size: 16px; line-height: 1.6;">
|
| 32 |
<h1>Text Generation</h1>
|
| 33 |
<p>
|
|
@@ -45,6 +45,134 @@ PAGE_HTML = """
|
|
| 45 |
</div>
|
| 46 |
"""
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
with gr.Blocks(head="""
|
| 49 |
<script>
|
| 50 |
document.addEventListener("mouseup", () => {
|
|
@@ -64,7 +192,22 @@ document.addEventListener("mouseup", () => {
|
|
| 64 |
</script>
|
| 65 |
""") as demo:
|
| 66 |
gr.Markdown("### 📘 Highlight text and ask GPT for help")
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
selected_text = gr.Textbox(
|
| 70 |
label="Selected text",
|
|
|
|
| 14 |
|
| 15 |
try:
|
| 16 |
response = client.chat.completions.create(
|
| 17 |
+
model="gpt-4o",
|
| 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\"\"\""}
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
return f"Error: {str(e)}"
|
| 28 |
|
| 29 |
+
# ---- Your work content ----
|
| 30 |
+
YOUR_WORK_HTML = """
|
| 31 |
<div id="content" style="max-width: 800px; margin: auto; font-size: 16px; line-height: 1.6;">
|
| 32 |
<h1>Text Generation</h1>
|
| 33 |
<p>
|
|
|
|
| 45 |
</div>
|
| 46 |
"""
|
| 47 |
|
| 48 |
+
# ---- Hugging Face reference content ----
|
| 49 |
+
HF_REFERENCE_HTML = """
|
| 50 |
+
<div id="hf-content" style="max-width: 800px; margin: auto; font-size: 16px; line-height: 1.6;">
|
| 51 |
+
<h1>Text Generation (Hugging Face Reference)</h1>
|
| 52 |
+
|
| 53 |
+
<h2>What is text generation?</h2>
|
| 54 |
+
<p>
|
| 55 |
+
Text generation is the task of generating new text given another text. These models can,
|
| 56 |
+
for example, fill in incomplete text or paraphrase.
|
| 57 |
+
</p>
|
| 58 |
+
|
| 59 |
+
<h2>Use Cases</h2>
|
| 60 |
+
|
| 61 |
+
<h3>Story Generation</h3>
|
| 62 |
+
<p>Models can be used to generate creative stories given a prompt.</p>
|
| 63 |
+
|
| 64 |
+
<h3>Code Generation</h3>
|
| 65 |
+
<p>Models can help developers by generating code snippets or entire functions.</p>
|
| 66 |
+
|
| 67 |
+
<h3>Text Completion</h3>
|
| 68 |
+
<p>Text generation models can autocomplete sentences or fill in missing words.</p>
|
| 69 |
+
|
| 70 |
+
<h2>Task Variants</h2>
|
| 71 |
+
|
| 72 |
+
<h3>Completion Generation Models</h3>
|
| 73 |
+
<p>
|
| 74 |
+
In this task, the model is trained to predict the next word(s) given an input sequence.
|
| 75 |
+
GPT-2 and GPT-3 are examples of this.
|
| 76 |
+
</p>
|
| 77 |
+
|
| 78 |
+
<h3>Text-to-Text Generation Models</h3>
|
| 79 |
+
<p>
|
| 80 |
+
Some text generation models are trained in a text-to-text manner. T5 and BART are examples
|
| 81 |
+
of this, where the model is trained to generate text given another text as input
|
| 82 |
+
(e.g., translation, summarization).
|
| 83 |
+
</p>
|
| 84 |
+
|
| 85 |
+
<h2>Inference</h2>
|
| 86 |
+
<p>
|
| 87 |
+
You can use the 🤗 Transformers library <code>text-generation</code> pipeline to do inference
|
| 88 |
+
with text generation models. It takes an input text and generates a continuation of that text.
|
| 89 |
+
</p>
|
| 90 |
+
<pre style="background: #f5f5f5; padding: 15px; border-radius: 5px; overflow-x: auto;">
|
| 91 |
+
from transformers import pipeline
|
| 92 |
+
|
| 93 |
+
generator = pipeline('text-generation', model='gpt2')
|
| 94 |
+
generator("Hello, I'm a language model,", max_length=30, num_return_sequences=3)
|
| 95 |
+
</pre>
|
| 96 |
+
|
| 97 |
+
<h2>Popular Models</h2>
|
| 98 |
+
<ul>
|
| 99 |
+
<li>GPT-2</li>
|
| 100 |
+
<li>GPT-Neo</li>
|
| 101 |
+
<li>GPT-J</li>
|
| 102 |
+
<li>BLOOM</li>
|
| 103 |
+
<li>LLaMA</li>
|
| 104 |
+
<li>Falcon</li>
|
| 105 |
+
<li>Mistral</li>
|
| 106 |
+
<li>Phi</li>
|
| 107 |
+
</ul>
|
| 108 |
+
<p>These models vary in size from millions to hundreds of billions of parameters.</p>
|
| 109 |
+
|
| 110 |
+
<h2>Example Applications</h2>
|
| 111 |
+
|
| 112 |
+
<h3>Creative Writing Assistant</h3>
|
| 113 |
+
<p>
|
| 114 |
+
Use text generation to help with creative writing by suggesting plot developments,
|
| 115 |
+
character dialogues, or descriptive passages.
|
| 116 |
+
</p>
|
| 117 |
+
|
| 118 |
+
<h3>Chatbots and Conversational AI</h3>
|
| 119 |
+
<p>Many modern chatbots use text generation models to provide natural, contextual responses.</p>
|
| 120 |
+
|
| 121 |
+
<h3>Content Creation</h3>
|
| 122 |
+
<p>Generate blog posts, product descriptions, social media content, and marketing copy.</p>
|
| 123 |
+
|
| 124 |
+
<h3>Programming Assistance</h3>
|
| 125 |
+
<p>
|
| 126 |
+
Code generation models can help developers write code faster by suggesting completions,
|
| 127 |
+
explaining code, or generating boilerplate.
|
| 128 |
+
</p>
|
| 129 |
+
|
| 130 |
+
<h2>Training</h2>
|
| 131 |
+
<p>
|
| 132 |
+
Text generation models are typically trained on large corpora of text using causal language
|
| 133 |
+
modeling, where the model learns to predict the next token given previous tokens.
|
| 134 |
+
</p>
|
| 135 |
+
|
| 136 |
+
<h3>Common training objectives:</h3>
|
| 137 |
+
<ul>
|
| 138 |
+
<li><strong>Causal Language Modeling (CLM):</strong> Predict next token (GPT-style)</li>
|
| 139 |
+
<li><strong>Masked Language Modeling (MLM):</strong> Predict masked tokens (BERT-style, though BERT isn't typically used for generation)</li>
|
| 140 |
+
<li><strong>Sequence-to-Sequence:</strong> Map input text to output text (T5, BART)</li>
|
| 141 |
+
</ul>
|
| 142 |
+
|
| 143 |
+
<h2>Metrics</h2>
|
| 144 |
+
<p>Common metrics for evaluating text generation:</p>
|
| 145 |
+
<ul>
|
| 146 |
+
<li><strong>Perplexity:</strong> Measures how well the model predicts the test data</li>
|
| 147 |
+
<li><strong>BLEU:</strong> Measures overlap with reference texts (common for translation)</li>
|
| 148 |
+
<li><strong>ROUGE:</strong> Measures overlap, often used for summarization</li>
|
| 149 |
+
<li><strong>Human Evaluation:</strong> Often the gold standard for creative tasks</li>
|
| 150 |
+
</ul>
|
| 151 |
+
|
| 152 |
+
<h2>Ethical Considerations</h2>
|
| 153 |
+
<p>Text generation models can:</p>
|
| 154 |
+
<ul>
|
| 155 |
+
<li>Generate biased or harmful content based on training data</li>
|
| 156 |
+
<li>Be misused to create misleading information or spam</li>
|
| 157 |
+
<li>Reproduce and amplify stereotypes present in training data</li>
|
| 158 |
+
</ul>
|
| 159 |
+
|
| 160 |
+
<p>It's important to:</p>
|
| 161 |
+
<ul>
|
| 162 |
+
<li>Carefully curate training data</li>
|
| 163 |
+
<li>Implement content filters</li>
|
| 164 |
+
<li>Be transparent about model capabilities and limitations</li>
|
| 165 |
+
<li>Consider the societal impact of deployed systems</li>
|
| 166 |
+
</ul>
|
| 167 |
+
</div>
|
| 168 |
+
"""
|
| 169 |
+
|
| 170 |
+
def switch_content(choice):
|
| 171 |
+
if choice == "My Work":
|
| 172 |
+
return YOUR_WORK_HTML
|
| 173 |
+
else:
|
| 174 |
+
return HF_REFERENCE_HTML
|
| 175 |
+
|
| 176 |
with gr.Blocks(head="""
|
| 177 |
<script>
|
| 178 |
document.addEventListener("mouseup", () => {
|
|
|
|
| 192 |
</script>
|
| 193 |
""") as demo:
|
| 194 |
gr.Markdown("### 📘 Highlight text and ask GPT for help")
|
| 195 |
+
|
| 196 |
+
# Toggle between your work and HF reference
|
| 197 |
+
view_toggle = gr.Radio(
|
| 198 |
+
choices=["My Work", "HF Reference"],
|
| 199 |
+
value="My Work",
|
| 200 |
+
label="View",
|
| 201 |
+
interactive=True
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
content_display = gr.HTML(YOUR_WORK_HTML)
|
| 205 |
+
|
| 206 |
+
view_toggle.change(
|
| 207 |
+
fn=switch_content,
|
| 208 |
+
inputs=view_toggle,
|
| 209 |
+
outputs=content_display
|
| 210 |
+
)
|
| 211 |
|
| 212 |
selected_text = gr.Textbox(
|
| 213 |
label="Selected text",
|