Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,35 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
# Cliente de inferência com modelo de IA pública
|
| 5 |
-
client = InferenceClient(model="meta-llama/Llama-3.3-70B-Instruct") # Modelo gratuito e avançado
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
mensagens = []
|
| 10 |
-
if historico is None:
|
| 11 |
-
historico = []
|
| 12 |
-
|
| 13 |
-
for item in historico:
|
| 14 |
-
if isinstance(item, list) and len(item) == 2:
|
| 15 |
-
user_msg, bot_msg = item
|
| 16 |
-
mensagens.append({"role": "user", "content": user_msg})
|
| 17 |
-
if bot_msg:
|
| 18 |
-
mensagens.append({"role": "assistant", "content": bot_msg})
|
| 19 |
-
|
| 20 |
-
mensagens.append({"role": "user", "content": mensagem})
|
| 21 |
-
resposta = ""
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
mensagens,
|
| 26 |
-
max_tokens=300,
|
| 27 |
-
stream=True,
|
| 28 |
-
temperature=0.4,
|
| 29 |
-
top_p=0.8,
|
| 30 |
-
):
|
| 31 |
-
if not mensagem or not isinstance(mensagem, dict):
|
| 32 |
-
continue
|
| 33 |
-
|
| 34 |
-
try:
|
| 35 |
-
conteudo = mensagem["choices"][0]["delta"].get("content", "")
|
| 36 |
-
if conteudo.strip():
|
| 37 |
-
resposta += conteudo
|
| 38 |
-
yield resposta
|
| 39 |
-
except (AttributeError, IndexError, KeyError) as e:
|
| 40 |
-
print(f"Erro ao processar mensagem: {e}")
|
| 41 |
-
continue
|
| 42 |
-
|
| 43 |
-
except Exception as e:
|
| 44 |
-
print(f"Erro inesperado: {e}")
|
| 45 |
-
yield "Ocorreu um erro ao gerar a resposta."
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
demo.launch()
|
|
|
|
| 1 |
+
from langchain.agents.openai_assistant import OpenAIAssistantRunnable
|
| 2 |
+
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
asst_id = os.getenv('assistant_key')
|
| 5 |
+
#key = os.getenv('open_ai')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
interpreter_assistant = OpenAIAssistantRunnable(assistant_id=asst_id)
|
| 8 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def chat_response(message, history):
|
| 11 |
+
output = interpreter_assistant.invoke({"content": message})
|
| 12 |
+
response = output[0].content[0].text.value
|
| 13 |
+
return response
|
| 14 |
+
css = """
|
| 15 |
+
label[data-testid="block-label"] {
|
| 16 |
+
display: none !important;
|
| 17 |
+
}
|
| 18 |
+
footer {
|
| 19 |
+
display: none !important;
|
| 20 |
+
}
|
| 21 |
+
"""
|
| 22 |
|
| 23 |
+
js_func = """
|
| 24 |
+
function refresh() {
|
| 25 |
+
const url = new URL(window.location);
|
| 26 |
+
if (url.searchParams.get('__theme') !== 'dark') {
|
| 27 |
+
url.searchParams.set('__theme', 'dark');
|
| 28 |
+
window.location.href = url.href;
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
"""
|
| 32 |
+
with gr.Blocks(css=css, js = js_func, theme="monochrome") as demo:
|
| 33 |
+
chatbot = gr.ChatInterface(chat_response,title="ProjSite")
|
| 34 |
|
| 35 |
+
demo.launch(share=True)
|
|
|