| import os |
| from langchain.memory import ConversationBufferMemory |
| import streamlit as st |
| import requests |
| from transformers import pipeline |
|
|
| |
| api_key = os.getenv("HUGGINGFACE_API_TOKEN") |
|
|
| |
| st.set_page_config( |
| page_title="Mi asistente personal de IA (By Daniela) 🤖", |
| page_icon="🤖") |
| st.title("Mi asistente personal de IA (By Daniela) 🤖") |
|
|
| |
| if "chat_history" not in st.session_state: |
| st.session_state.chat_history = [ |
| {"role": "assistant", "content": "Hola soy tu asistente virtual, ¿en qué puedo ayudarte hoy?"} |
| ] |
|
|
| |
| |
| model_class = st.selectbox( |
| "Selecciona el modelo que deseas usar:", |
| options=["ollama", "openai", "hf_hub"], |
| index=0 |
| ) |
|
|
| temperature = st.slider("Temperatura", 0.0, 1.0, 0.5, 0.05) |
| max_tokens = st.slider("Longitud máxima de respuesta (tokens)", 10, 1024, 100, 10) |
|
|
| |
| |
| """def model_hf_hub(model="google/shieldgemma-2b", temperature=0.1): |
| llm = HuggingFaceEndpoint( |
| repo_id=model, |
| temperature=temperature, |
| max_new_tokens=64, |
| return_full_text=False, |
| huggingfacehub_api_token=api_key, |
| model_kwargs={ |
| "stop": ["<|endoftext|>", "<|eot_id|>"], # Tokens de parada |
| } |
| ) |
| return llm""" |
|
|
| def model_hf_hub(model="google/shieldgemma-2b", temperature=0.1): |
| |
| llm = pipeline("text-generation", model=model, tokenizer=model) |
| return llm |
| |
| def model_openai(model="gpt-4o-mini", temperature=0.1): |
| llm = ChatOpenAI( |
| model=model, |
| temperature=temperature, |
| |
| max_tokens=100, |
| frequency_penalty = 0.3 |
| ) |
| return llm |
|
|
| def model_ollama(model="phi3:latest", temperature=0.1,): |
| llm = ChatOllama( |
| model=model, |
| temperature=temperature, |
| base_url="http://127.0.0.1:11434" |
| ) |
| return llm |
|
|
| |
| def detectar_idioma(text): |
| try: |
| return detect(text) |
| except: |
| return "error" |
| |
| def model_response(user_query,chat_history, model_class): |
| |
| |
| idioma = detectar_idioma(user_query) |
| print(f"Idioma detectado: {idioma}") |
|
|
| |
| idioma_nombres = { |
| "es": "Spanish", |
| "en": "English", |
| "fr": "French", |
| "de": "German", |
| "it": "Italian", |
| "pt": "Portuguese" |
| } |
| |
| language = idioma_nombres.get(idioma, "Spanish") |
| |
| |
| st.write(f"Idioma detectado: {language}") |
| |
| |
| |
| |
| if model_class == "ollama": |
| llm = model_ollama() |
| elif model_class == "openai": |
| llm = model_openai() |
| elif model_class == "hf_hub": |
| llm = model_hf_hub() |
| else: |
| raise ValueError("Invalid model class") |
| |
| |
| memory = ConversationBufferMemory(return_messages=True) |
|
|
| |
| for message in chat_history: |
| if message['role'] == 'user': |
| memory.chat_memory.add_message(HumanMessage(content=message['content'])) |
| elif message["role"] == "assistant": |
| memory.chat_memory.add_message(AIMessage(content=message['content'])) |
|
|
| |
| system_prompt = "You are a helpful assistant answering general questions. Please respond in {language}." |
| |
| language = "spanish" |
| user_prompt = "{input}" |
| |
| prompt_template = ChatPromptTemplate.from_messages([ |
| ("system", system_prompt), |
| |
| ("user", user_prompt) |
| ]) |
|
|
| |
| chain = prompt_template | llm | StrOutputParser() |
|
|
| |
| response = chain.stream({ |
| "input": user_query, |
| "language": language |
| }) |
|
|
| |
| return response |
|
|
| |
| user_query = st.chat_input("Introduce mensaje...") |
|
|
| |
| |
| if user_query and user_query.strip(): |
| |
| st.session_state.chat_history.append({"role": "user", "content": user_query}) |
|
|
| |
| with st.chat_message("Human"): |
| st.write(user_query) |
|
|
| |
| response_stream = model_response(user_query, st.session_state.chat_history, model_class) |
|
|
| |
| with st.chat_message("AI"): |
| respuesta_completa = st.write_stream(response_stream) |
|
|
| |
| |
| st.session_state.chat_history.append({"role": "assistant", "content": respuesta_completa}) |