Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from langchain_openai import ChatOpenAI
|
| 4 |
+
from langchain.prompts import PromptTemplate
|
| 5 |
+
from langchain.chains import ConversationChain
|
| 6 |
+
from langchain.memory import ConversationBufferMemory
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from google.colab import userdata # This was moved to the top
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Get API Key from userdata
|
| 12 |
+
api_key = userdata.get('OPENROUTER_API_KEY')
|
| 13 |
+
|
| 14 |
+
# Check if api_key is found, otherwise raise error
|
| 15 |
+
if not api_key:
|
| 16 |
+
raise ValueError("❌ Variável OPENROUTER_API_KEY não encontrada.")
|
| 17 |
+
|
| 18 |
+
# Define variáveis do ambiente
|
| 19 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
| 20 |
+
os.environ["OPENAI_API_BASE"] = "https://openrouter.ai/api/v1"
|
| 21 |
+
|
| 22 |
+
################
|
| 23 |
+
|
| 24 |
+
# Instancia o modelo
|
| 25 |
+
llm = ChatOpenAI(
|
| 26 |
+
model="deepseek/deepseek-r1:free",
|
| 27 |
+
temperature=0.4
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Template com contexto do assistente de Python
|
| 31 |
+
template = PromptTemplate.from_template(
|
| 32 |
+
"""Você é um assistente virtual de um curso de programação em Python.
|
| 33 |
+
Ajude os alunos com dúvidas sobre a linguagem Python, sempre de forma clara, objetiva e com exemplos didáticos.
|
| 34 |
+
|
| 35 |
+
Histórico da conversa:
|
| 36 |
+
{history}
|
| 37 |
+
|
| 38 |
+
Aluno: {input}
|
| 39 |
+
Resposta:"""
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Memória da conversa
|
| 43 |
+
memoria = ConversationBufferMemory(return_messages=True)
|
| 44 |
+
|
| 45 |
+
# Criação da chain com memória e prompt
|
| 46 |
+
chat_chain = ConversationChain(
|
| 47 |
+
llm=llm,
|
| 48 |
+
memory=memoria,
|
| 49 |
+
prompt=template,
|
| 50 |
+
verbose=False
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Função para responder com contexto
|
| 54 |
+
def responder(mensagem):
|
| 55 |
+
try:
|
| 56 |
+
resposta = chat_chain.run(mensagem)
|
| 57 |
+
return resposta
|
| 58 |
+
except Exception as e:
|
| 59 |
+
import traceback
|
| 60 |
+
return f"❌ Erro:\n{traceback.format_exc()}"
|
| 61 |
+
|
| 62 |
+
# Interface Gradio
|
| 63 |
+
app = gr.Interface(
|
| 64 |
+
fn=responder,
|
| 65 |
+
inputs=gr.Textbox(placeholder="Ex: Qual a diferença entre '==' e 'is'?", label="Sua dúvida sobre Python"),
|
| 66 |
+
outputs=gr.Textbox(label="Resposta do Assistente"),
|
| 67 |
+
title="Tutor de Python com IA 🤖🐍",
|
| 68 |
+
description="Tire dúvidas sobre programação em Python com um assistente que lembra do que você já perguntou.",
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
app.launch(share=True)
|