File size: 1,439 Bytes
ab02808
 
 
 
efe183a
 
 
 
ab02808
 
 
 
 
 
efe183a
ab02808
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import transformers
import torch

import os

hf_key = os.getenv("HF_TOKEN")

# Initialize the model
model_id = "bmi-labmedinfo/Igea-350M-v0.0.1"
pipeline = transformers.pipeline(
    "text-generation",
    model=model_id,
    model_kwargs={"torch_dtype": torch.bfloat16},
    token=hf_key
)

# Define the function to generate text
def generate_text(input_text, max_new_tokens=128, temperature=1.0, top_k=50, top_p=0.95):
    output = pipeline(
        input_text,
        max_new_tokens=max_new_tokens,
        temperature=temperature,
        top_k=top_k,
        top_p=top_p,
    )
    return output[0]['generated_text']

# Create the Gradio interface
iface = gr.Interface(
    fn=generate_text,
    inputs=[
        gr.inputs.Textbox(lines=2, placeholder="Enter your text here...", label="Input Text"),
        gr.inputs.Slider(minimum=1, maximum=200, default=128, step=1, label="Max New Tokens"),
        gr.inputs.Slider(minimum=0.1, maximum=2.0, default=1.0, step=0.1, label="Temperature"),
        gr.inputs.Slider(minimum=1, maximum=100, default=50, step=1, label="Top-k"),
        gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.95, step=0.01, label="Top-p")
    ],
    outputs="text",
    title="Text Generation Interface",
    description="Enter a prompt to generate text using the Igea-350M model and adjust the hyperparameters."
)

# Launch the interface
if __name__ == "__main__":
    iface.launch()