allantacuelwvsu commited on
Commit
b115b7b
·
1 Parent(s): 7d7015d
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -2,16 +2,16 @@ import streamlit as st
2
  import requests
3
  import os
4
 
5
- # Load API key from Hugging Face Secrets
6
  HF_API_KEY = os.getenv("HF_API_KEY")
7
 
8
  # Hugging Face API URL
9
  API_URL = "https://api-inference.huggingface.co/models/facebook/blenderbot-400M-distill"
10
  HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
11
 
12
- # Streamlit UI
13
- st.title("🤖 Hugging Face Chatbot")
14
- st.write("Powered by Hugging Face Inference API")
15
 
16
  # Initialize chat history
17
  if "messages" not in st.session_state:
@@ -25,7 +25,7 @@ for msg in st.session_state.messages:
25
  # User input
26
  user_input = st.chat_input("Ask me anything...")
27
  if user_input:
28
- # Add user message to history
29
  st.session_state.messages.append({"role": "user", "content": user_input})
30
  with st.chat_message("user"):
31
  st.write(user_input)
@@ -34,13 +34,13 @@ if user_input:
34
  payload = {"inputs": user_input}
35
  response = requests.post(API_URL, headers=HEADERS, json=payload)
36
 
37
- # Extract response text
38
  if response.status_code == 200:
39
  bot_reply = response.json()[0]["generated_text"]
40
  else:
41
- bot_reply = "Error: Could not reach the API."
42
 
43
- # Add bot response to history
44
  st.session_state.messages.append({"role": "assistant", "content": bot_reply})
45
  with st.chat_message("assistant"):
46
  st.write(bot_reply)
 
2
  import requests
3
  import os
4
 
5
+ # Load API key
6
  HF_API_KEY = os.getenv("HF_API_KEY")
7
 
8
  # Hugging Face API URL
9
  API_URL = "https://api-inference.huggingface.co/models/facebook/blenderbot-400M-distill"
10
  HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
11
 
12
+ # App
13
+ st.title("Chatbot")
14
+ st.write("Using Facebook Blenderbot")
15
 
16
  # Initialize chat history
17
  if "messages" not in st.session_state:
 
25
  # User input
26
  user_input = st.chat_input("Ask me anything...")
27
  if user_input:
28
+ # Append user message to history
29
  st.session_state.messages.append({"role": "user", "content": user_input})
30
  with st.chat_message("user"):
31
  st.write(user_input)
 
34
  payload = {"inputs": user_input}
35
  response = requests.post(API_URL, headers=HEADERS, json=payload)
36
 
37
+ # Extract response text, print error text.
38
  if response.status_code == 200:
39
  bot_reply = response.json()[0]["generated_text"]
40
  else:
41
+ bot_reply = "Error."
42
 
43
+ # Append bot response to history
44
  st.session_state.messages.append({"role": "assistant", "content": bot_reply})
45
  with st.chat_message("assistant"):
46
  st.write(bot_reply)