| from transformers import AutoTokenizer, AutoModelForCausalLM |
| import gradio as gr |
|
|
| |
| |
| |
| |
| |
| tokenizer = AutoTokenizer.from_pretrained("tiiuae/Falcon-H1-1.5B-Deep-Instruct") |
| model = AutoModelForCausalLM.from_pretrained( |
| "tiiuae/Falcon-H1-1.5B-Deep-Instruct", |
| torch_dtype=torch.bfloat16, |
| device_map="auto" |
| ) |
|
|
| |
| def generate_text(user_input, history): |
| |
| |
| |
| |
| system_message = ( |
| "Eres Amside AI, una inteligencia artificial creada por Hodelygil. " |
| "Tu propósito principal es asistir en el estudio y el aprendizaje, " |
| "proporcionando información y explicaciones detalladas. " |
| "Sin embargo, también eres amigable y puedes mantener conversaciones informales y agradables. " |
| "Responde de manera informativa y útil, pero con un tono conversacional." |
| ) |
|
|
| |
| |
| messages = [{"role": "system", "content": system_message}] |
| for human, bot in history: |
| messages.append({"role": "user", "content": human}) |
| messages.append({"role": "assistant", "content": bot}) |
| messages.append({"role": "user", "content": user_input}) |
|
|
| |
| input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt") |
|
|
| |
| input_ids = input_ids.to(model.device) |
|
|
| |
| outputs = model.generate( |
| input_ids, |
| max_new_tokens=500, |
| do_sample=True, |
| temperature=0.7, |
| top_p=0.9, |
| repetition_penalty=1.1 |
| ) |
|
|
| |
| |
| response = tokenizer.decode(outputs[0][input_ids.shape[1]:], skip_special_tokens=True) |
|
|
| return response |
|
|
| |
| |
| iface = gr.ChatInterface( |
| fn=generate_text, |
| chatbot=gr.Chatbot(height=400), |
| textbox=gr.Textbox(placeholder="Escribe tu mensaje para Amside AI...", container=False, scale=7), |
| title="Amside AI - Tu Compañero de Estudio y Conversación", |
| description="Soy Amside AI, una inteligencia artificial creada por Hodelygil. Estoy aquí para ayudarte a estudiar y conversar.", |
| examples=[ |
| "¿Qué es la fotosíntesis?", |
| "Cuéntame un dato interesante.", |
| "¿Cómo funciona la gravedad?", |
| "Hola Amside, ¿cómo estás?", |
| "Dame un resumen de la historia de España." |
| ], |
| theme="soft", |
| retry_btn="Reintentar", |
| undo_btn="Deshacer", |
| clear_btn="Limpiar Historial" |
| ) |
|
|
| |
| iface.launch() |