fehmikaya commited on
Commit
ed4daa4
·
verified ·
1 Parent(s): ccf25db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -25
app.py CHANGED
@@ -9,6 +9,8 @@ st.title("Llama Chatbot")
9
  # Get the API key from the environment variable
10
  API_KEY = os.getenv("HF_TOKEN")
11
 
 
 
12
  if API_KEY is None:
13
  st.error("API key not found. Please set the HF_TOKEN secret in your Hugging Face Space.")
14
  st.stop()
@@ -20,14 +22,6 @@ headers = {
20
  "Authorization": f"Bearer {API_KEY}"
21
  }
22
 
23
- def response_generator():
24
- response = requests.post(API_URL, headers=headers, json={"inputs": json.dumps(st.session_state.messages)})
25
- print("JSON:"+json.dumps(st.session_state.messages))
26
- if response.status_code == 200:
27
- yield response.json()[0]['generated_text']
28
- else:
29
- yield "Error: Unable to fetch response from the model."
30
-
31
  if "messages" not in st.session_state:
32
  st.session_state.messages = [{"role": "system", "content":
33
  """
@@ -37,21 +31,36 @@ if "messages" not in st.session_state:
37
  """
38
  }]
39
 
40
- # Display chat messages from history on app rerun
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  for message in st.session_state.messages:
42
- with st.chat_message(message["role"]):
43
- st.markdown(message["content"])
44
-
45
- # Accept user input
46
- if prompt := st.chat_input("What is up?"):
47
- # Display user message in chat message container
48
- with st.chat_message("user"):
49
- st.markdown(prompt)
50
- # Add user message to chat history
51
- st.session_state.messages.append({"role": "user", "content": prompt})
52
-
53
- # Display assistant response in chat message container
54
- with st.chat_message("assistant"):
55
- response = st.write_stream(response_generator())
56
- # Add assistant response to chat history
57
- st.session_state.messages.append({"role": "assistant", "content": response})
 
9
  # Get the API key from the environment variable
10
  API_KEY = os.getenv("HF_TOKEN")
11
 
12
+ icons = {"assistant": "robot.png", "user": "man-kddi.png"}
13
+
14
  if API_KEY is None:
15
  st.error("API key not found. Please set the HF_TOKEN secret in your Hugging Face Space.")
16
  st.stop()
 
22
  "Authorization": f"Bearer {API_KEY}"
23
  }
24
 
 
 
 
 
 
 
 
 
25
  if "messages" not in st.session_state:
26
  st.session_state.messages = [{"role": "system", "content":
27
  """
 
31
  """
32
  }]
33
 
34
+ def handle_query(query):
35
+ response = requests.post(API_URL, headers=headers, json={"inputs": json.dumps(st.session_state.messages)})
36
+ print("JSON:"+json.dumps(st.session_state.messages))
37
+ if response.status_code == 200:
38
+ return response.json()[0]['generated_text']
39
+ else:
40
+ return "Error: Unable to fetch response from the model."
41
+
42
+ def streamer(text):
43
+ for i in text:
44
+ yield i
45
+ time.sleep(0.001)
46
+
47
+ if 'messages' not in st.session_state:
48
+ st.session_state.messages = [{'role': 'assistant', "content": 'ask me anything'}]
49
+
50
  for message in st.session_state.messages:
51
+ with st.chat_message(message['role'], avatar=icons[message['role']]):
52
+ st.write(message['content'])
53
+
54
+ user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
55
+
56
+ if user_prompt and (uploaded_file):
57
+ st.session_state.messages.append({'role': 'user', "content": user_prompt})
58
+ with st.chat_message("user", avatar="man-kddi.png"):
59
+ st.write(user_prompt)
60
+
61
+ # Trigger assistant's response retrieval and update UI
62
+ with st.spinner("Thinking..."):
63
+ response = handle_query(user_prompt)
64
+ with st.chat_message("user", avatar="robot.png"):
65
+ st.write_stream(streamer(response))
66
+ st.session_state.messages.append({'role': 'assistant', "content": response})