File size: 1,279 Bytes
bdbc895
 
5ed2f8b
bdbc895
7818faa
 
 
 
 
bdbc895
 
5ed2f8b
 
 
 
 
bdbc895
7818faa
 
bdbc895
 
7818faa
bdbc895
7818faa
 
 
bdbc895
 
7818faa
bdbc895
 
 
7818faa
 
 
 
 
 
 
 
 
 
 
bdbc895
 
 
39cb8e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from transformers import pipeline, set_seed
import gradio as gr
import re

generator = pipeline(
    "text-generation",
    model="Keyurjotaniya007/openai-gpt2-wikitext-textgen"
)

set_seed(42)

def clean_output(text):
    text = re.sub(r"=+\s*[^=]+?\s*=+", "", text)
    text = re.sub(r"\s+", " ", text).strip()
    return text

def generate_text(prompt):
    if not prompt.strip():
        return "Please enter a prompt to generate text."
    response = generator(
        prompt,
        max_length=150,
        num_return_sequences=1,
        temperature=0.8,
        top_p=0.95,
        do_sample=True,
        truncation=True
    )
    return response[0]["generated_text"]

interface = gr.Interface(
    fn=generate_text,
    inputs=gr.Textbox(lines=4, placeholder="Type your prompt here..."),
    outputs=gr.Textbox(label="Generated Text"),
    title="🦄 GPT-2 WikiText Text Generator",
    description=(
        "Generate coherent, fluent English text using **GPT-2 fine-tuned on the WikiText dataset**. "
        "Model: `Keyurjotaniya007/openai-gpt2-wikitext-textgen`"
    ),
    examples=[
        ["Artificial intelligence is transforming the world by"],
        ["Once upon a time, in a small village,"],
    ],
)

if __name__ == "__main__":
    interface.launch()