File size: 3,791 Bytes
cd7e83a
7cc87ca
82ea2ae
cd7e83a
7cc87ca
cd7e83a
ce16a38
cd7e83a
 
7cc87ca
ce16a38
cd7e83a
ce16a38
 
 
 
 
 
 
 
 
 
 
 
 
 
a672d1b
ce16a38
 
 
 
 
 
82ea2ae
ce16a38
 
82ea2ae
ce16a38
 
82ea2ae
ce16a38
 
82ea2ae
ce16a38
 
82ea2ae
ce16a38
 
 
 
 
 
 
 
82ea2ae
ce16a38
82ea2ae
ce16a38
 
 
 
 
 
 
 
 
 
 
7cc87ca
 
ce16a38
82ea2ae
5a2fb73
 
 
cd7e83a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'''import os
import gradio as gr
from transformers import pipeline
from huggingface_hub import InferenceClient

hf_token = os.getenv("gpt2_token")
# Initialize the text generation pipeline
client = 
generator = pipeline("text-generation", )

# Define the response function with additional options for customization
def text_generation(
    prompt: str,
    details: bool = False,
    stream: bool = False,
    model: str = None,
    best_of: int = None,
    decoder_input_details: bool = None,
    do_sample: bool = False,
    frequency_penalty: float = None,
    grammar: None = None,
    max_new_tokens: int = None,
    repetition_penalty: float = None
):
    # Setup the configuration for the model generation
    gen_params = {
        "max_length": 518,  # Default, you can tweak it or set from parameters
        "num_return_sequences": 1,
        "do_sample": do_sample,
        "temperature": 0.7,  # Controls randomness
        "top_k": 50,  # You can adjust for more control over sampling
        "top_p": 0.9,  # Same as above, for sampling
    }

    if max_new_tokens:
        gen_params["max_length"] = max_new_tokens + len(prompt.split())

    if frequency_penalty:
        gen_params["frequency_penalty"] = frequency_penalty

    if repetition_penalty:
        gen_params["repetition_penalty"] = repetition_penalty

    # Generate the text based on the input prompt and parameters
    generated_text = generator(prompt, **gen_params)[0]["generated_text"]

    if details:
        # Return additional details for debugging if needed
        return {
            "generated_text": generated_text,
            "params_used": gen_params
        }
    else:
        return generated_text

# Create Gradio interface
iface = gr.Interface(
    fn=text_generation,  # The function we defined
    inputs=[
        gr.Textbox(label="Input Prompt"),  # User input prompt
        gr.Checkbox(label="Show Details", default=False),  # Option for additional details
        gr.Checkbox(label="Stream Mode", default=False),  # Streaming checkbox (not used in this example)
        gr.Textbox(label="Model (optional)", default=None),  # Optional model name
        gr.Slider(minimum=1, maximum=5, label="Best of (Optional)", default=None),
        gr.Slider(minimum=0.0, maximum=2.0, label="Frequency Penalty (Optional)", default=None),
        gr.Slider(minimum=0.0, maximum=2.0, label="Repetition Penalty (Optional)", default=None),
    ],
    outputs="text"  # Output is plain text
)

# Launch the interface
iface.launch()
'''

import gradio as gr
from transformers import pipeline

# Load a text generation model (e.g., GPT-2 or a model of your choice)
generator = pipeline("text-generation", model="isitcoding/gpt2_120_finetuned", tokenizer="isitcoding/gpt2_120_finetuned")

# Function to generate assistant's response based on user input
def generate_response(messages):
    # Extract the user message from the input format
    user_message = messages[-1]["content"]
    
    # Generate a response based on the user's input
    response = generator(user_message, max_length=100, num_return_sequences=1)
    
    # Get the assistant's message from the generated output
    assistant_message = response[0]["generated_text"]
    
    # Return the updated conversation with user and assistant messages
    messages.append({"role": "assistant", "content": assistant_message})
    return messages

# Set up the Gradio interface
iface = gr.Interface(
    fn=generate_response,
    inputs=gr.Textbox(placeholder="Type your message..."),  # User input
    outputs=gr.JSON(),  # JSON format output to display the conversation
    live=True,  # Ensure real-time updates
    title="Text Generation Pipeline",
    description="Enter a message to get a response from the assistant."
)

iface.launch()