File size: 1,203 Bytes
3e8f413
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM
import spaces

# Load the model and configuration
config = PeftConfig.from_pretrained("diabolic6045/gemma-2-2b-chess-adapter")
base_model = AutoModelForCausalLM.from_pretrained("google/gemma-2-2b")
model = PeftModel.from_pretrained(base_model, "diabolic6045/gemma-2-2b-chess-adapter")

# Define a function that takes user input and returns the model's output
@spaces.GPU(duration=120)
def generate_text(prompt):
    input_ids = model.tokenizer.encode(prompt, return_tensors="pt")
    output = model.generate(input_ids, max_length=100)
    return model.tokenizer.decode(output[0], skip_special_tokens=True)

# Create a Gradio interface
demo = gr.Interface(
    fn=generate_text,
    inputs=gr.Textbox(label="Input prompt"),
    outputs=gr.Textbox(label="Generated text"),
    title="Chess Text Generation with Gemma-2-2B",
    description="Enter a prompt and the model will generate a response.",
    examples=[
        ["What is the best opening move in chess?"],
        ["What is the Ruy Lopez opening?"],
        ["What is the Sicilian Defense?"],
    ],
)

# Launch the Gradio app
demo.launch()