| import os |
| from huggingface_hub import login |
| from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig |
| import gradio as gr |
|
|
| |
| hf_token = os.getenv("LLAMA31") |
|
|
| if hf_token: |
| |
| login(token=hf_token) |
| else: |
| raise ValueError("Hugging Face token no encontrado. Aseg煤rate de que la variable de entorno HF_TOKEN est茅 configurada.") |
|
|
| |
| bnb_config = BitsAndBytesConfig(load_in_4bit=True) |
|
|
| |
| model_id = "meta-llama/Meta-Llama-3.1-8B-Instruct" |
| tokenizer = AutoTokenizer.from_pretrained(model_id) |
| model = AutoModelForCausalLM.from_pretrained( |
| model_id, |
| device_map="auto", |
| quantization_config=bnb_config |
| ) |
|
|
| |
| def chat_fn(multimodal_message): |
| |
| question = multimodal_message["text"] |
| |
| |
| conversation = [{"role": "user", "content": question}] |
|
|
| |
| input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt") |
| input_ids = input_ids.to(model.device) |
| |
| |
| streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True) |
|
|
| |
| generate_kwargs = dict( |
| input_ids=input_ids, |
| streamer=streamer, |
| max_new_tokens=500, |
| do_sample=True, |
| temperature=0.7, |
| ) |
|
|
| |
| t = Thread(target=model.generate, kwargs=generate_kwargs) |
| t.start() |
|
|
| |
| message = "" |
| for text in streamer: |
| message += text |
| yield message |
|
|
| |
| with gr.Blocks() as demo: |
| |
| gr.Markdown("# 馃攳 Chatbot Analizador de Documentos") |
| |
| |
| response = gr.Textbox(lines=5, label="Respuesta") |
| |
| |
| chat = gr.MultimodalTextbox(file_types=["image"], interactive=True, |
| show_label=False, placeholder="Sube una imagen del documento haciendo clic en '+' y haz una pregunta.") |
| |
| |
| chat.submit(chat_fn, inputs=chat, outputs=response) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |
|
|
|
|
|
|