File size: 2,292 Bytes
5101e06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

# Initialize model and tokenizer
checkpoint = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)

def generate_response(prompt, max_tokens, temperature, top_p):
    try:
        # Format input as chat message
        messages = [{"role": "user", "content": prompt}]
        input_text = tokenizer.apply_chat_template(messages, tokenize=False)
        
        # Encode and generate
        inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
        outputs = model.generate(
            inputs,
            max_new_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            do_sample=True
        )
        
        # Decode and return response
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        return response
    except Exception as e:
        return f"Error: {str(e)}"

# Create Gradio interface
iface = gr.Interface(
    fn=generate_response,
    inputs=[
        gr.Textbox(
            label="Enter your prompt",
            placeholder="What would you like to know?",
            lines=3
        ),
        gr.Slider(
            minimum=10,
            maximum=200,
            value=50,
            step=10,
            label="Maximum Tokens"
        ),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.2,
            step=0.1,
            label="Temperature"
        ),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.9,
            step=0.1,
            label="Top P"
        )
    ],
    outputs=gr.Textbox(label="Generated Response", lines=5),
    title="SmolLM2-1.7B-Instruct Demo",
    description="Generate responses using the SmolLM2-1.7B-Instruct model",
    examples=[
        ["What is the capital of France?", 50, 0.2, 0.9],
        ["Explain quantum computing in simple terms.", 100, 0.3, 0.9],
        ["Write a short poem about nature.", 150, 0.7, 0.9]
    ]
)

# Launch the application
if __name__ == "__main__":
    iface.launch(share=True)