From 829a475123d7b3218e88bbab1cfde2c188240807 Mon Sep 17 00:00:00 2001 From: faiber1986 Date: Fri, 6 Mar 2026 14:33:28 -0500 Subject: [PATCH] =?UTF-8?q?Archivo=20con=20interfaz=20en=20streamlit=20par?= =?UTF-8?q?a=20consultor=20t=C3=A9cnico.=20Para=20su=20despliegue=20es=20n?= =?UTF-8?q?ecesario=20instalar=20las=20dependencias=20necesarias=20en=20tu?= =?UTF-8?q?=20ambiente=20(en=20mi=20caso=20estoy=20usando=20uv=20como=20ge?= =?UTF-8?q?stor)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- week1/app.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 week1/app.py diff --git a/week1/app.py b/week1/app.py new file mode 100644 index 0000000000..ba601483de --- /dev/null +++ b/week1/app.py @@ -0,0 +1,73 @@ +import streamlit as st +import ollama +from openai import OpenAI +from dotenv import load_dotenv + +# load environment variables +load_dotenv() + +# initialize OpenAI client +openai_client = OpenAI() + +MODEL_GPT = 'gpt-4o-mini' +MODEL_LLAMA = 'llama3.2' + +# Constants +SYSTEM_PROMPT = "Eres un tutor técnico útil que responde preguntas sobre código Python, ingeniería de software, ciencia de datos y LLM" + +st.title("Tutor Técnico LLM 🤖") +st.markdown("Haz una pregunta técnica y selecciona el modelo con el que quieres obtener tu respuesta.") + +# Create the layout +model_choice = st.selectbox( + "Selecciona el modelo:", + (MODEL_GPT, MODEL_LLAMA) +) + +question = st.text_area("Ingresa tu pregunta técnica aquí:", height=150) + +if st.button("Enviar"): + if not question.strip(): + st.warning("Por favor, ingresa una pregunta antes de enviar.") + else: + user_prompt = "Por favor, da una explicación detallada de la siguiente pregunta: " + question + + messages = [ + {"role": "system", "content": SYSTEM_PROMPT}, + {"role": "user", "content": user_prompt} + ] + + st.subheader("Respuesta:") + + if model_choice == MODEL_GPT: + # We use an empty placeholder to stream the response + response_placeholder = st.empty() + full_response = "" + + with st.spinner("Esperando respuesta de GPT-4o-mini..."): + try: + stream = openai_client.chat.completions.create( + model=MODEL_GPT, + messages=messages, + stream=True + ) + + for chunk in stream: + content = chunk.choices[0].delta.content or '' + full_response += content + # We progressively write the response markdown + response_placeholder.markdown(full_response + "▌") + + # Remove the cursor at the end + response_placeholder.markdown(full_response) + except Exception as e: + st.error(f"Error al llamar a OpenAI: {e}") + + elif model_choice == MODEL_LLAMA: + with st.spinner("Esperando respuesta de Llama 3.2..."): + try: + response = ollama.chat(model=MODEL_LLAMA, messages=messages) + reply = response['message']['content'] + st.markdown(reply) + except Exception as e: + st.error(f"Error al llamar a Ollama: {e}")