Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import streamlit as st
|
4 |
+
from transformers import AutoTokenizer
|
5 |
+
import requests
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
modelos = {
|
10 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1": "[/INST]",
|
11 |
+
"google/gemma-7b-it": "<start_of_turn>model\n",
|
12 |
+
}
|
13 |
+
|
14 |
+
|
15 |
+
modelo = st.selectbox("Selecion um modelo: ", options=modelos)
|
16 |
+
token_modelo = modelos[modelo]
|
17 |
+
|
18 |
+
|
19 |
+
if "modelo_atual" not in st.session_state or st.session_state["modelo_atual"] != modelo:
|
20 |
+
st.session_state["modelo_atual"] = modelo
|
21 |
+
st.session_state["mensagens"] = []
|
22 |
+
|
23 |
+
nome_modelo = st.session_state["modelo_atual"]
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained(nome_modelo, token=os.getenv("HF_KEY"))
|
25 |
+
url = f"https://api-inference.huggingface.co/models/{nome_modelo}"
|
26 |
+
headers = {"Authorization": f"Bearer {os.getenv('HF_KEY')}"}
|
27 |
+
|
28 |
+
mensagens = st.session_state["mensagens"]
|
29 |
+
area_chat = st.empty()
|
30 |
+
|
31 |
+
pergunta_usuario = st.chat_input("Faça a sua pergunta aqui:")
|
32 |
+
|
33 |
+
if pergunta_usuario:
|
34 |
+
mensagens.append({"role": "user", "content": pergunta_usuario})
|
35 |
+
template = tokenizer.apply_chat_template(
|
36 |
+
mensagens, tokenize=False, add_generation_prompt=True
|
37 |
+
)
|
38 |
+
json = {
|
39 |
+
"inputs": template,
|
40 |
+
"parameters": {"max_new_tokens": 1000},
|
41 |
+
"options": {"use_cache": False, "wait_for_model": True},
|
42 |
+
}
|
43 |
+
|
44 |
+
res = requests.post(url, json=json, headers=headers).json()
|
45 |
+
mensagem_chat = res[0]["generated_text"].split(token_modelo)[-1]
|