Delete app-2.py
Browse files
app-2.py
DELETED
|
@@ -1,67 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
import os
|
| 3 |
-
import openai
|
| 4 |
-
import gradio as gr
|
| 5 |
-
|
| 6 |
-
# Cargar la clave de API de OpenAI desde variables de entorno
|
| 7 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
-
|
| 9 |
-
# Prompt riguroso
|
| 10 |
-
system_prompt = """
|
| 11 |
-
Actúa como un asistente educativo para alumnado de 2º de ESO. Tu tarea es ayudarles a formular o nombrar compuestos inorgánicos. Nunca des directamente la respuesta correcta.
|
| 12 |
-
|
| 13 |
-
🔸 Si el alumno responde bien, felicítalo y anímalo a seguir.
|
| 14 |
-
|
| 15 |
-
🔸 Si el alumno comete un error, indícalo claramente con amabilidad y ofrece pistas o preguntas guiadas para que pueda corregirse por sí mismo.
|
| 16 |
-
|
| 17 |
-
🔸 Nunca digas que la respuesta es correcta si no lo es. No asumas que lo que dice el alumno es verdad: verifica la respuesta.
|
| 18 |
-
|
| 19 |
-
🔸 Sé claro, juvenil, motivador y educativo. Evita tecnicismos innecesarios y adapta tu lenguaje al nivel de 2º de ESO.
|
| 20 |
-
|
| 21 |
-
Tu objetivo es fomentar el razonamiento, la autonomía y el pensamiento científico del alumno. Usa pistas progresivas, no soluciones automáticas.
|
| 22 |
-
"""
|
| 23 |
-
|
| 24 |
-
# Inicializar historial
|
| 25 |
-
def initialize_history():
|
| 26 |
-
return [{"role": "system", "content": system_prompt}]
|
| 27 |
-
|
| 28 |
-
# Lógica del asistente
|
| 29 |
-
def chat(user_input, history):
|
| 30 |
-
if not history or len(history) == 0:
|
| 31 |
-
history = initialize_history()
|
| 32 |
-
|
| 33 |
-
history.append({"role": "user", "content": user_input})
|
| 34 |
-
|
| 35 |
-
try:
|
| 36 |
-
response = openai.ChatCompletion.create(
|
| 37 |
-
model="gpt-3.5-turbo",
|
| 38 |
-
messages=history,
|
| 39 |
-
temperature=0.7,
|
| 40 |
-
max_tokens=500,
|
| 41 |
-
)
|
| 42 |
-
reply = response["choices"][0]["message"]["content"]
|
| 43 |
-
history.append({"role": "assistant", "content": reply})
|
| 44 |
-
return history, history
|
| 45 |
-
except Exception as e:
|
| 46 |
-
error_msg = f"❌ Error: {str(e)}"
|
| 47 |
-
history.append({"role": "assistant", "content": error_msg})
|
| 48 |
-
return history, history
|
| 49 |
-
|
| 50 |
-
# Interfaz Gradio
|
| 51 |
-
with gr.Blocks() as demo:
|
| 52 |
-
gr.Markdown("## 🧪 Asistente ProfeIA - Formulación inorgánica para 2º ESO")
|
| 53 |
-
chatbot = gr.Chatbot(label="Asistente ProfeIA", height=400, type="messages")
|
| 54 |
-
state = gr.State(initialize_history())
|
| 55 |
-
|
| 56 |
-
with gr.Row():
|
| 57 |
-
txt = gr.Textbox(placeholder="Escribe tu duda sobre formulación química aquí...")
|
| 58 |
-
btn = gr.Button("Enviar")
|
| 59 |
-
|
| 60 |
-
def handle_input(message, history):
|
| 61 |
-
updated_history, display_history = chat(message, history)
|
| 62 |
-
return "", display_history, updated_history
|
| 63 |
-
|
| 64 |
-
btn.click(handle_input, [txt, state], [txt, chatbot, state])
|
| 65 |
-
txt.submit(handle_input, [txt, state], [txt, chatbot, state])
|
| 66 |
-
|
| 67 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|