File size: 1,258 Bytes
0874cc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

# Load the model and tokenizer
model_name = "mrcuddle/SD-Prompter-1B"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Function to generate a response
def chat(message, history):
    # Combine the message and history into a single input
    input_text = " ".join([f"{user}: {msg}" for user, msg in history] + [f"User: {message}"])
    inputs = tokenizer(input_text, return_tensors="pt")

    # Generate a response
    with torch.no_grad():
        outputs = model.generate(inputs.input_ids, max_length=50, num_return_sequences=1)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)

    # Extract only the new response part
    response = response.replace(input_text, "").strip()

    # Append the new message and response to the history
    history.append(("User", message))
    history.append(("Assistant", response))

    return history, history

# Create the Gradio chat interface
iface = gr.ChatInterface(
    fn=chat,
    title="Llama3.2 1B Stable Diffusion Prompter",
    description="Generate Stable Diffusion Prompt with Llama3.2"
)

# Launch the interface
iface.launch()