Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# Set the model ID of your fine-tuned model on Hugging Face
|
5 |
+
MODEL_ID = "Mishal23/fine-tuned-dialoGPT-crm-chatbot" # Your model ID
|
6 |
+
|
7 |
+
# Retrieve your Hugging Face token from secrets
|
8 |
+
HUGGING_FACE_TOKEN = st.secrets["HUGGING_FACE_TOKEN"]
|
9 |
+
|
10 |
+
# Function to generate a response from the chatbot using the Hugging Face API
|
11 |
+
def generate_response(prompt):
|
12 |
+
headers = {"Authorization": f"Bearer {HUGGING_FACE_TOKEN}"}
|
13 |
+
payload = {"inputs": prompt}
|
14 |
+
|
15 |
+
try:
|
16 |
+
# Make the API call to the Hugging Face model
|
17 |
+
response = requests.post(f"https://api-inference.huggingface.co/models/{MODEL_ID}", headers=headers, json=payload)
|
18 |
+
response.raise_for_status() # Raise an error for bad responses
|
19 |
+
return response.json()[0]['generated_text']
|
20 |
+
except requests.exceptions.HTTPError as http_err:
|
21 |
+
st.error(f"HTTP error occurred: {http_err}")
|
22 |
+
return "Sorry, there was an error with the server."
|
23 |
+
except requests.exceptions.RequestException as req_err:
|
24 |
+
st.error(f"Request error occurred: {req_err}")
|
25 |
+
return "Sorry, there was an issue with your request."
|
26 |
+
except Exception as e:
|
27 |
+
st.error(f"Error generating response: {e}")
|
28 |
+
return "Sorry, I couldn't generate a response."
|
29 |
+
|
30 |
+
# Streamlit UI setup
|
31 |
+
st.title("Chatbot Powered by Hugging Face")
|
32 |
+
st.subheader("Talk to the Chatbot")
|
33 |
+
|
34 |
+
# User input
|
35 |
+
user_input = st.text_input("You: ", "")
|
36 |
+
|
37 |
+
# Button to submit the input
|
38 |
+
if st.button("Send"):
|
39 |
+
if user_input:
|
40 |
+
with st.spinner("Generating response..."):
|
41 |
+
bot_response = generate_response(user_input)
|
42 |
+
st.text_area("Chatbot:", value=bot_response, height=200)
|
43 |
+
else:
|
44 |
+
st.warning("Please enter a message before sending.")
|