File size: 1,517 Bytes
b258782
be8df6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b258782
 
be8df6a
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# Carica il modello e il tokenizer
model_name = "meta-llama/Llama-3.1-8B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Funzione per generare risposte
def generate_response(prompt):
    inputs = tokenizer(prompt, return_tensors="pt")
    outputs = model.generate(**inputs, max_length=150)
    response = tokenizer.decode(outputs, skip_special_tokens=True)
    return response

# CSS personalizzato
css = """
body {font-family: Arial, sans-serif; background-color: #f4f4f9; color: #333;}
.gradio-container {border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);}
input[type="text"] {border: 1px solid #ccc; border-radius: 5px; padding: 10px; width: 100%;}
button {background-color: #4CAF50; color: white; border: none; border-radius: 5px; padding: 10px 20px; cursor: pointer;}
button:hover {background-color: #45a049;}
"""

# Interfaccia Gradio
with gr.Blocks(css=css) as demo:
    gr.Markdown("# Chatbot con Llama-3.1-8B-Instruct")
    with gr.Row():
        with gr.Column():
            user_input = gr.Textbox(label="Tu:", placeholder="Scrivi qui il tuo messaggio...")
            submit_button = gr.Button("Invia")
        with gr.Column():
            chatbot_output = gr.Textbox(label="Chatbot:", interactive=False)
    
    submit_button.click(generate_response, inputs=user_input, outputs=chatbot_output)

# Avvia l'interfaccia
demo.launch()