carlotamdeluna commited on
Commit
cfc401a
1 Parent(s): 339c71f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -42
app.py CHANGED
@@ -1,41 +1,24 @@
1
- import subprocess
2
 
3
- # Instalar un paquete utilizando pip desde Python
4
- subprocess.check_call(["pip", "install", "langchain_community","langchain"])
5
 
6
- import streamlit as st
7
- from langchain.chains import LLMChain
8
- from langchain.chains import ConversationChain
9
- #importacipones adicionales
10
  import streamlit as st
11
  from langchain.chains import ConversationChain
12
- from langchain.memory import ConversationBufferMemory
13
- from langchain.chains import ConversationChain
14
- from langchain.prompts import PromptTemplate
15
-
16
  from langchain.chains.conversation.memory import ConversationEntityMemory
17
  from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
18
- from langchain_community.llms import HuggingFaceEndpoint
19
  import os
20
- from langchain.memory import ConversationBufferWindowMemory
21
-
22
-
23
- st.set_page_config(page_title= "bot", layout="wide")
24
 
25
- #Interfaz
26
- st.title("Maqueta")
27
 
28
- #template
29
 
30
- #llm
31
 
32
- llm = HuggingFaceEndpoint(repo_id='tiiuae/falcon-7b',
33
- model_kwargs = {"max_length":64},
34
- temperature=0.5,
35
- huggingfacehub_api_token = os.environ["HUGGINGFACEHUB_API_TOKEN"])
36
-
37
- #memory
38
 
 
 
 
39
  if "generated" not in st.session_state:
40
  st.session_state["generated"] = []
41
  if "past" not in st.session_state:
@@ -45,9 +28,7 @@ if "input" not in st.session_state:
45
  if "stored_session" not in st.session_state:
46
  st.session_state["stored_session"] = []
47
 
48
-
49
-
50
-
51
  def get_text():
52
  """
53
  Get the user input text.
@@ -60,24 +41,84 @@ def get_text():
60
  label_visibility='hidden')
61
  return input_text
62
 
63
- user_input = get_text()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
 
66
- #op1
67
- memory = ConversationBufferMemory()
68
- Conversation = ConversationChain(llm = llm,
69
- memory = memory)
70
 
71
- #opc2:BUFFER WINDOW MEMORY
72
- memory2 = ConversationBufferWindowMemory(k=3) #k hace referencia al numero de conversaiones que queremos almacenar
73
- conversation_buff2 = ConversationChain(llm = llm,
74
- memory = memory2)
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
 
78
- submit = st.button("Generate")
79
 
80
- while submit:
81
- output = Conversation.invoke(input=user_input)
82
-
 
83
 
 
 
 
 
 
 
1
 
 
 
2
 
3
+ # Import necessary libraries
 
 
 
4
  import streamlit as st
5
  from langchain.chains import ConversationChain
 
 
 
 
6
  from langchain.chains.conversation.memory import ConversationEntityMemory
7
  from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
 
8
  import os
9
+ from getpass import getpass
10
+ from langchain import HuggingFaceHub
11
+ from langchain_community.llms import HuggingFaceEndpoint
 
12
 
 
 
13
 
14
+ #token de hugging face
15
 
16
+ os.environ['HUGGINGFACEHUB_API_TOKEN'] = "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"
17
 
 
 
 
 
 
 
18
 
19
+ # Set Streamlit page configuration
20
+ st.set_page_config(page_title='🧠MemoryBot🤖', layout='wide')
21
+ # Initialize session states. Un session state es como un diccionario
22
  if "generated" not in st.session_state:
23
  st.session_state["generated"] = []
24
  if "past" not in st.session_state:
 
28
  if "stored_session" not in st.session_state:
29
  st.session_state["stored_session"] = []
30
 
31
+ # Define function to get user input
 
 
32
  def get_text():
33
  """
34
  Get the user input text.
 
41
  label_visibility='hidden')
42
  return input_text
43
 
44
+ # #parte para hacer un chat nuevo
45
+ def new_chat():
46
+ """
47
+ Clears session state and starts a new chat.
48
+ """
49
+ save = []
50
+ for i in range(len(st.session_state['generated'])-1, -1, -1):
51
+ save.append("User:" + st.session_state["past"][i])
52
+ save.append("Bot:" + st.session_state["generated"][i])
53
+ st.session_state["stored_session"].append(save)
54
+ st.session_state["generated"] = []
55
+ st.session_state["past"] = []
56
+ st.session_state["input"] = ""
57
+ st.session_state.entity_memory.entity_store = {}
58
+ st.session_state.entity_memory.buffer.clear()
59
+
60
+ # Add a button to start a new chat
61
+ st.sidebar.button("New Chat", on_click = new_chat, type='primary')
62
 
63
 
 
 
 
 
64
 
 
 
 
 
65
 
66
+ # Move K outside of the sidebar expander
67
+ K = st.sidebar.number_input(' (#)Summary of prompts to consider', min_value=3, max_value=1000)
68
+
69
+ # Set up the Streamlit app layout
70
+ st.title("Personalized chatbot")
71
+
72
+
73
+
74
+ # Create an OpenAI instance
75
+ llm = HuggingFaceEndpoint(repo_id='meta-llama/Llama-2-7b-chat-hf',
76
+ max_length=128,
77
+ temperature=0.5,
78
+ token="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")
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+ # Create a ConversationEntityMemory object if not already created
87
+ if 'entity_memory' not in st.session_state:
88
+ st.session_state.entity_memory = ConversationEntityMemory(llm=llm, k=K )
89
+
90
+ # Create the ConversationChain object with the specified configuration
91
+ Conversation = ConversationChain(
92
+ llm=llm,
93
+ prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE,
94
+ memory=st.session_state.entity_memory
95
+ )
96
+
97
+
98
+ # Get the user input
99
+ user_input = get_text()
100
+
101
+ # Generate the output using the ConversationChain object and the user input, and add the input/output to the session
102
+ if user_input:
103
+ output = Conversation.run(input=user_input)
104
+ st.session_state.past.append(user_input)
105
+ st.session_state.generated.append(output)
106
+
107
+
108
+ # Display the conversation history using an expander, and allow the user to download it
109
+ with st.expander("Conversation", expanded=True):
110
+ for i in range(len(st.session_state['generated'])-1, -1, -1):
111
+ st.info(st.session_state["past"][i],icon="🧐")
112
+ st.success(st.session_state["generated"][i], icon="🤖")
113
 
114
 
 
115
 
116
+ # Display stored conversation sessions in the sidebar
117
+ for i, sublist in enumerate(st.session_state.stored_session):
118
+ with st.sidebar.expander(label= f"Conversation-Session:{i}"):
119
+ st.write(sublist)
120
 
121
+ # Allow the user to clear all stored conversation sessions
122
+ if st.session_state.stored_session:
123
+ if st.sidebar.checkbox("Clear-all"):
124
+ del st.session_state.stored_session