vonliechti commited on
Commit
1742d80
·
verified ·
1 Parent(s): 7b4ec76

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +11 -31
app.py CHANGED
@@ -54,9 +54,9 @@ def add_message(message, messages):
54
  return messages
55
 
56
  def interact_with_agent(messages, request: Request):
57
- username = request.username
58
  prompt = messages[-1]['content']
59
- agent.logs = sessions.get(username + "_logs", [])
60
  for msg in stream_from_transformers_agent(agent, prompt):
61
  messages.append(msg)
62
  yield messages
@@ -65,17 +65,17 @@ def interact_with_agent(messages, request: Request):
65
  def persist(component):
66
 
67
  def resume_session(value, request: Request):
68
- username = request.username
69
- print(f"Resuming session for {username}")
70
- state = sessions.get(username, value)
71
- agent.logs = sessions.get(username + "_logs", [])
72
  return state
73
 
74
  def update_session(value, request: Request):
75
- username = request.username
76
- print(f"Updating persisted session state for {username}")
77
- sessions[username] = value
78
- sessions[username + "_logs"] = agent.logs
79
  pickle.dump(sessions, open(sessions_path, "wb"))
80
  return
81
 
@@ -84,16 +84,7 @@ def persist(component):
84
 
85
  return component
86
 
87
- def welcome_message(request: Request):
88
- return f"<h2>Welcome, {request.username}</h2>"
89
-
90
  with gr.Blocks(fill_height=True) as demo:
91
- # put the welcome message and logout button in a row
92
- with gr.Row() as row:
93
- welcome_msg = gr.Markdown(f"Welcome")
94
- logout_button = gr.Button("Logout", link="/logout")
95
- demo.load(welcome_message, None, welcome_msg)
96
-
97
  chatbot = persist(gr.Chatbot(
98
  value=[],
99
  label="SQuAD Agent",
@@ -129,16 +120,5 @@ with gr.Blocks(fill_height=True) as demo:
129
  interact_with_agent, [chatbot], [chatbot]
130
  )
131
 
132
- def honor_system_auth(username, password):
133
- return password == "happy"
134
-
135
  if __name__ == "__main__":
136
- demo.launch(
137
- auth=honor_system_auth,
138
- auth_message="""<h3>Honor System Authentication:</h3>
139
- <p>Log in with <strong>any username</strong> and the password <strong>"happy"</strong>.</p>
140
- <br/><br/>
141
- <i>Note:Your chat history will be saved to your username, but take care because others
142
- may log in with the same username and see your chat history.</i>
143
- """,
144
- )
 
54
  return messages
55
 
56
  def interact_with_agent(messages, request: Request):
57
+ session_hash = request.session_hash
58
  prompt = messages[-1]['content']
59
+ agent.logs = sessions.get(session_hash + "_logs", [])
60
  for msg in stream_from_transformers_agent(agent, prompt):
61
  messages.append(msg)
62
  yield messages
 
65
  def persist(component):
66
 
67
  def resume_session(value, request: Request):
68
+ session_hash = request.session_hash
69
+ print(f"Resuming session for {session_hash}")
70
+ state = sessions.get(session_hash, value)
71
+ agent.logs = sessions.get(session_hash + "_logs", [])
72
  return state
73
 
74
  def update_session(value, request: Request):
75
+ session_hash = request.session_hash
76
+ print(f"Updating persisted session state for {session_hash}")
77
+ sessions[session_hash] = value
78
+ sessions[session_hash + "_logs"] = agent.logs
79
  pickle.dump(sessions, open(sessions_path, "wb"))
80
  return
81
 
 
84
 
85
  return component
86
 
 
 
 
87
  with gr.Blocks(fill_height=True) as demo:
 
 
 
 
 
 
88
  chatbot = persist(gr.Chatbot(
89
  value=[],
90
  label="SQuAD Agent",
 
120
  interact_with_agent, [chatbot], [chatbot]
121
  )
122
 
 
 
 
123
  if __name__ == "__main__":
124
+ demo.launch()