File size: 1,884 Bytes
d70e310
c245a44
 
d70e310
c245a44
197a245
d70e310
c245a44
 
d70e310
971836c
 
c245a44
971836c
 
c245a44
 
971836c
d70e310
971836c
c245a44
 
 
 
 
971836c
 
 
 
 
 
 
c245a44
 
 
b6b3a04
 
971836c
 
d70e310
b6b3a04
c245a44
971836c
d70e310
 
 
c245a44
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

# Load the tokenizer and model from Hugging Face
model_name = "waterdrops0/mistral-nouns400"

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)

def generate_text(prompt, max_length=50, temperature=0.7, repetition_penalty=1.2):
    # Encode the input prompt
    inputs = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
    
    # Generate output based on the prompt with repetition penalty
    outputs = model.generate(
        inputs,
        max_length=max_length + inputs.shape[1],  # Ensuring generated text extends beyond the input prompt
        temperature=temperature,
        repetition_penalty=repetition_penalty,  # Add repetition penalty
        do_sample=True,
        top_p=0.95,
        top_k=60
    )

    # Decode the generated tokens, skipping the input tokens
    generated_tokens = outputs[0, inputs.shape[1]:]  # Only get the new tokens
    generated_text = tokenizer.decode(generated_tokens, skip_special_tokens=True)

    return generated_text

# Update the Gradio interface to include repetition penalty slider
iface = gr.Interface(
    fn=generate_text,
    inputs=[
        gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt"),
        gr.Slider(10, 200, step=10, value=50, label="Max Length"),
        gr.Slider(0.1, 1.0, step=0.1, value=0.7, label="Temperature"),
        gr.Slider(1.0, 2.0, step=0.1, value=1.2, label="Repetition Penalty")  # Add a slider for repetition penalty
    ],
    outputs=gr.Textbox(label="Generated Text"),
    title="Mistral 7B Nouns Model",
    description="Generate text using the fine-tuned Mistral 7B model with repetition penalty."
)

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