hf
Browse files- .gitignore +1 -0
- app.py +78 -0
- danigpt.png +0 -0
- requirements.txt +4 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import time
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
from PIL import Image
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
st.set_page_config(layout="centered")
|
11 |
+
|
12 |
+
col1, col2 = st.columns([2,3])
|
13 |
+
|
14 |
+
with col1:
|
15 |
+
image = Image.open('danigpt.png')
|
16 |
+
st.image(image, width=200)
|
17 |
+
|
18 |
+
with col2:
|
19 |
+
st.title("DaniGPT 🤖")
|
20 |
+
st.write("Preguntame cualquier cosa sobre python, Judini o Langchain")
|
21 |
+
st.write("OJO 👀 No estoy conectado a internet, así que puedo alucinar 😵💫, es tu trabajo corroborar que la respuesta que te entregue sea correcta.")
|
22 |
+
st.markdown('---')
|
23 |
+
# Initialize chat history
|
24 |
+
if "messages" not in st.session_state:
|
25 |
+
st.session_state.messages = []
|
26 |
+
|
27 |
+
# Display chat messages from history on app rerun
|
28 |
+
for message in st.session_state.messages:
|
29 |
+
with st.chat_message(message["role"]):
|
30 |
+
st.markdown(message["content"])
|
31 |
+
|
32 |
+
# Accept user input
|
33 |
+
if prompt := st.chat_input("En que te puedo ayudar?"):
|
34 |
+
# Add user message to chat history
|
35 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
36 |
+
# Display user message in chat message container
|
37 |
+
with st.chat_message("user"):
|
38 |
+
st.markdown(prompt)
|
39 |
+
|
40 |
+
# Display assistant response in chat message container
|
41 |
+
with st.chat_message("assistant"):
|
42 |
+
message_placeholder = st.empty()
|
43 |
+
full_response = ""
|
44 |
+
#Judini
|
45 |
+
api_key= os.getenv("JUDINI_API_KEY")
|
46 |
+
agent_id= os.getenv("JUDINI_AGENT_ID")
|
47 |
+
url = 'https://playground.judini.ai/api/v1/agent/'+agent_id
|
48 |
+
headers = {"Content-Type": "application/json; charset=utf-8", "Authorization": "Bearer "+api_key}
|
49 |
+
data = {
|
50 |
+
"messages": [
|
51 |
+
{
|
52 |
+
"role": "user",
|
53 |
+
"content": prompt
|
54 |
+
}
|
55 |
+
]
|
56 |
+
}
|
57 |
+
response = requests.post(url, headers=headers, json=data, stream=True)
|
58 |
+
raw_data = ''
|
59 |
+
tokens = ''
|
60 |
+
for chunk in response.iter_content(chunk_size=1024):
|
61 |
+
if chunk:
|
62 |
+
raw_data = chunk.decode('utf-8').replace("data: ", '')
|
63 |
+
if raw_data != "":
|
64 |
+
lines = raw_data.strip().splitlines()
|
65 |
+
for line in lines:
|
66 |
+
line = line.strip()
|
67 |
+
if line and line != "[DONE]":
|
68 |
+
try:
|
69 |
+
json_object = json.loads(line)
|
70 |
+
result = json_object['data']
|
71 |
+
full_response += result
|
72 |
+
time.sleep(0.05)
|
73 |
+
# Add a blinking cursor to simulate typing
|
74 |
+
message_placeholder.markdown(full_response + "▌")
|
75 |
+
except json.JSONDecodeError:
|
76 |
+
print(f'Error al decodificar el objeto JSON en la línea: {line}')
|
77 |
+
message_placeholder.markdown(full_response)
|
78 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
danigpt.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
requests
|
3 |
+
python-dotenv
|
4 |
+
Pillow
|