Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
for message in st.session_state.messages:
|
| 42 |
-
with st.chat_message(message[
|
| 43 |
-
st.
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
st.session_state.messages.append({
|
|
|
|
| 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})
|