Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Create a text-generation pipeline using GPT-2
|
| 5 |
generator = pipeline('text-generation', model='gpt2')
|
| 6 |
|
| 7 |
def generate_text(prompt):
|
| 8 |
-
# Use a lower temperature and limit the max_length for concise output
|
| 9 |
generated = generator(
|
| 10 |
prompt,
|
| 11 |
-
max_length=30,
|
| 12 |
-
do_sample=
|
| 13 |
-
temperature=0.
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
return generated[0]['generated_text']
|
| 16 |
|
| 17 |
-
# Create a Gradio interface
|
| 18 |
iface = gr.Interface(
|
| 19 |
fn=generate_text,
|
| 20 |
inputs="text",
|
| 21 |
outputs="text",
|
| 22 |
title="Simple LLM with Hugging Face & Gradio",
|
| 23 |
-
description="Enter a prompt and get a concise
|
| 24 |
)
|
| 25 |
|
| 26 |
-
# Launch the interface
|
| 27 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Create a text-generation pipeline using GPT-2
|
| 5 |
generator = pipeline('text-generation', model='gpt2')
|
| 6 |
|
| 7 |
def generate_text(prompt):
|
|
|
|
| 8 |
generated = generator(
|
| 9 |
prompt,
|
| 10 |
+
max_length=30, # Limit the output length
|
| 11 |
+
do_sample=True, # Enable sampling for more natural responses
|
| 12 |
+
temperature=0.3, # Lower temperature for less randomness
|
| 13 |
+
repetition_penalty=1.5, # Penalize repeated tokens
|
| 14 |
+
no_repeat_ngram_size=2 # Avoid repeating any 2-word sequences
|
| 15 |
)
|
| 16 |
return generated[0]['generated_text']
|
| 17 |
|
|
|
|
| 18 |
iface = gr.Interface(
|
| 19 |
fn=generate_text,
|
| 20 |
inputs="text",
|
| 21 |
outputs="text",
|
| 22 |
title="Simple LLM with Hugging Face & Gradio",
|
| 23 |
+
description="Enter a prompt and get a concise, factual answer."
|
| 24 |
)
|
| 25 |
|
|
|
|
| 26 |
iface.launch()
|