Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline, set_seed
|
3 |
+
|
4 |
+
# Initialize the text-generation pipeline with DistilGPT2 on CPU.
|
5 |
+
generator = pipeline("text-generation", model="distilgpt2", device=-1)
|
6 |
+
set_seed(42)
|
7 |
+
|
8 |
+
def generate_text(prompt, stop_choice):
|
9 |
+
# Map the dropdown choice to an actual stop character.
|
10 |
+
mapping = {"Period": ".", "Space": " "}
|
11 |
+
stop_token = mapping.get(stop_choice, "")
|
12 |
+
# Generate text – we set max_length relative to the prompt length.
|
13 |
+
output = generator(prompt, max_length=len(prompt.split()) + 50, num_return_sequences=1)[0]["generated_text"]
|
14 |
+
# Look for the stop token in the generated output (after the prompt).
|
15 |
+
idx = output.find(stop_token, len(prompt))
|
16 |
+
if idx != -1:
|
17 |
+
# If found, return the output up to and including the stop token.
|
18 |
+
return output[: idx + len(stop_token)]
|
19 |
+
else:
|
20 |
+
# Otherwise, return the full generated text.
|
21 |
+
return output
|
22 |
+
|
23 |
+
# Define a simple Gradio interface:
|
24 |
+
iface = gr.Interface(
|
25 |
+
fn=generate_text,
|
26 |
+
inputs=[
|
27 |
+
gr.inputs.Textbox(lines=4, placeholder="Enter your prompt here...", label="Prompt"),
|
28 |
+
gr.inputs.Dropdown(choices=["Period", "Space"], label="Stop Token")
|
29 |
+
],
|
30 |
+
outputs="text",
|
31 |
+
title="DistilGPT2 Text Generation",
|
32 |
+
description="Enter a prompt and select a stop token (Period or Space) to halt generation."
|
33 |
+
)
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
iface.launch()
|