File size: 1,434 Bytes
315691e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

# Load your model and tokenizer
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "ahmedbasemdev/LLama3.2-fine-tuned"  # Replace with your model name
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

def single_inference(question):
    messages = []

    messages.append({"role": "user", "content": question})

    input_ids = tokenizer.apply_chat_template(
        messages,
        add_generation_prompt=True,
        return_tensors="pt"
    ).to(model.device)

    terminators = [
        tokenizer.eos_token_id,
        tokenizer.convert_tokens_to_ids("<|eot_id|>")
    ]

    outputs = model.generate(
        input_ids,
        max_new_tokens=256,
        eos_token_id=terminators,
        do_sample=True,
        temperature=0.2,
    )
    response = outputs[0][input_ids.shape[-1]:]
    output = tokenizer.decode(response, skip_special_tokens=True)
    return output

# Create the Gradio interface
interface = gr.Interface(
    fn=single_inference,  # Function to wrap
    inputs=gr.Textbox(lines=2, placeholder="Ask a question..."),  # Input type
    outputs=gr.Textbox(label="Response"),  # Output type
    title="Chat with Your Model",  # App title
    description="Enter a question, and the model will generate a response.",  # App description
)

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