File size: 2,739 Bytes
e8e247e
956b24f
081b46f
956b24f
 
 
 
 
 
 
 
 
814e7e2
956b24f
 
 
 
 
 
 
 
 
df2a22b
956b24f
 
 
 
 
 
6f1af31
956b24f
 
 
 
7b8870b
956b24f
814e7e2
956b24f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
import gradio as gr
from transformers import pipeline

# Initialize a dictionary of models for each strategy
models = {
    "GPT-2": "gpt2",
    "ChatGPT": "EleutherAI/gpt-neo-1.3",
    "LLaMa": "model_for_LLaMa",
    "Vicuna": "model_for_Vicuna",
    "Alpaca": "model_for_Alpaca",
    "Flan-T5": "model_for_Flan-T5",
}

# Define a function to generate text based on the selected model
def generate_text(input_instruction, selected_model):
    if selected_model in models:
        model_name = models[selected_model]
        pipe = pipeline("text-generation", model=model_name)
        generated_text = pipe(input_instruction, max_length=100, do_sample=True)[0]['generated_text']
        return generated_text
    else:
        return "Please select a model for this strategy."

# Define example instructions for testing
instruction_examples = [
    ("Write a short story about a cat."),
    ("Explain the concept of artificial intelligence."),
    ("Compose a poem about nature."),
]

# Create a Gradio interface
iface = gr.Interface(
    fn=generate_text,
    inputs=gr.Textbox(placeholder="Enter instruction here..."),
    outputs=gr.Textbox(),
    examples=instruction_examples,
    live=True,
    title="Text Generation with Dynamic Model Selection",
)

# Additional input section 1 - User input
with gr.Row():
    user_input = gr.Textbox(placeholder="Enter your input...")

# Additional input section 2 - Strategy 1
with gr.Row():
    strategy1_selector = gr.Dropdown(list(models.keys()), label="Strategy 1 - QA-Based Prompting")

# Additional input section 3 - Strategy 2
with gr.Row():
    strategy2_selector = gr.Dropdown(list(models.keys()), label="Strategy 2 - Instruction-Based Prompting")

# Additional input section 4 - Strategy 3
with gr.Row():
    strategy3_selector = gr.Dropdown(list(models.keys()), label="Strategy 3 - Structured Prompting")

# Create a callback function for dynamic model selection
def update_model_and_generate_text(input_instruction, selected_model):
    if selected_model:
        selected_model_name = strategy1_selector.value if selected_model == "Strategy 1 - QA-Based Prompting" else (
            strategy2_selector.value if selected_model == "Strategy 2 - Instruction-Based Prompting" else
            strategy3_selector.value)
        iface.set_function(generate_text, inputs=[user_input, selected_model], outputs="outputs")
        return "Selected model: " + selected_model_name

# Add a submit button to trigger dynamic model selection
submit_button = gr.Button("Submit")

# Set the function for the Gradio interface to the update_model_and_generate_text function
iface.fn = update_model_and_generate_text

# Add the submit button to the interface
iface.add(submit_button)

iface.launch()