File size: 2,299 Bytes
c52adb8
 
28afd39
 
eceeded
 
 
 
 
 
28afd39
 
 
 
 
 
 
 
eceeded
 
 
 
 
 
 
 
 
7e8ebae
 
 
 
 
 
eceeded
 
 
 
 
 
 
7e8ebae
 
 
eceeded
7e8ebae
 
eceeded
 
 
 
7e8ebae
eceeded
 
 
 
 
 
28afd39
 
 
eceeded
7e8ebae
eceeded
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from text_generator import TextGenerationTool

# Create an instance of the tool with a safer default model
text_gen_tool = TextGenerationTool(default_model="distilgpt2")

# Launch the Gradio interface
if __name__ == "__main__":
    import gradio as gr
    
    with gr.Blocks(title="Text Generation Tool") as demo:
        # Add a warning about authentication
        gr.Markdown("""
        # Text Generation Tool
        
        > **Note:** This application can run without a Hugging Face token, but some models may require authentication.
        > For best results with larger models, set the `HF_TOKEN` environment variable with your token.
        """)

        
        with gr.Row():
            with gr.Column():
                prompt_input = gr.Textbox(
                    label="Enter your prompt", 
                    placeholder="Write a short story about a robot learning to paint.",
                    lines=5
                )
                
                model_dropdown = gr.Dropdown(
                    choices=list(text_gen_tool.models.keys()),
                    value=text_gen_tool.default_model,
                    label="Select Model"
                )
                
                with gr.Row():
                    generate_btn = gr.Button("Generate Text")
                    clear_btn = gr.Button("Clear")
            
            with gr.Column():
                output = gr.Textbox(label="Generated Text", lines=15)
        
        def generate_with_model(prompt, model_key):
            return text_gen_tool.generate_text(prompt, model_key)
        
        generate_btn.click(
            fn=generate_with_model,
            inputs=[prompt_input, model_dropdown],
            outputs=output
        )
        
        clear_btn.click(
            fn=lambda: ("", None),
            inputs=None,
            outputs=[prompt_input, output]
        )
        
        gr.Examples(
            examples=[
                ["Write a short story about a robot learning to paint.", "distilgpt2"],
                ["Explain quantum computing to a 10-year-old.", "gpt2-small"],
                ["Write a poem about the changing seasons.", "distilgpt2"]
            ],
            inputs=[prompt_input, model_dropdown]
        )
    
    demo.launch(share=True)