Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,23 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Set up Streamlit UI
|
| 8 |
st.title("Learning Chatbot")
|
|
@@ -22,10 +37,10 @@ if user_message:
|
|
| 22 |
# Combine the history for context in the conversation
|
| 23 |
conversation_history = " ".join(st.session_state.history)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
bot_response =
|
| 27 |
|
| 28 |
-
#
|
| 29 |
st.session_state.history.append(f"Bot: {bot_response}")
|
| 30 |
|
| 31 |
# Show conversation history
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Set your Hugging Face API key here
|
| 5 |
+
API_KEY = 'your_huggingface_api_key'
|
| 6 |
+
MODEL_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"
|
| 7 |
+
|
| 8 |
+
# Function to get chatbot response from Hugging Face API
|
| 9 |
+
def query_huggingface_api(message):
|
| 10 |
+
headers = {"Authorization": f"Bearer {API_KEY}"}
|
| 11 |
+
payload = {"inputs": message}
|
| 12 |
+
|
| 13 |
+
response = requests.post(MODEL_URL, headers=headers, json=payload)
|
| 14 |
+
response_json = response.json()
|
| 15 |
+
|
| 16 |
+
# Extract and return the chatbot response
|
| 17 |
+
if isinstance(response_json, list):
|
| 18 |
+
return response_json[0]['generated_text']
|
| 19 |
+
else:
|
| 20 |
+
return "Error: Unable to get response from the model."
|
| 21 |
|
| 22 |
# Set up Streamlit UI
|
| 23 |
st.title("Learning Chatbot")
|
|
|
|
| 37 |
# Combine the history for context in the conversation
|
| 38 |
conversation_history = " ".join(st.session_state.history)
|
| 39 |
|
| 40 |
+
# Query the Hugging Face API to get the response
|
| 41 |
+
bot_response = query_huggingface_api(conversation_history)
|
| 42 |
|
| 43 |
+
# Append the bot's response to the history
|
| 44 |
st.session_state.history.append(f"Bot: {bot_response}")
|
| 45 |
|
| 46 |
# Show conversation history
|