Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,51 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
|
| 5 |
# Streamlit interface
|
| 6 |
-
st.title("Chat with
|
| 7 |
-
st.write("This chatbot is powered by a
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
| 14 |
-
model = GPT2LMHeadModel.from_pretrained(model_name)
|
| 15 |
-
return tokenizer, model
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
user_input = st.text_input("You:", "Type your message here...")
|
| 28 |
|
| 29 |
if st.button("Send"):
|
| 30 |
if user_input:
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
user_input,
|
| 35 |
-
max_length=128,
|
| 36 |
-
num_return_sequences=1,
|
| 37 |
-
temperature=0.7,
|
| 38 |
-
pad_token_id=tokenizer.eos_token_id
|
| 39 |
-
)
|
| 40 |
-
bot_response = response[0]['generated_text']
|
| 41 |
-
|
| 42 |
-
# Clean up the response by removing the input text if it appears
|
| 43 |
-
if user_input in bot_response:
|
| 44 |
-
bot_response = bot_response.replace(user_input, "").strip()
|
| 45 |
-
|
| 46 |
-
st.write(f"Bot: {bot_response}")
|
| 47 |
except Exception as e:
|
| 48 |
-
st.error(f"Error: {str(e)}")
|
| 49 |
else:
|
| 50 |
st.write("Please enter a message.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from langchain_community.llms import HuggingFaceHub
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
# Streamlit interface
|
| 6 |
+
st.title("Chat with Hugging Face Model")
|
| 7 |
+
st.write("This chatbot is powered by a model hosted on Hugging Face Hub.")
|
| 8 |
|
| 9 |
+
# Note: You need to set HF_TOKEN as environment variable
|
| 10 |
+
if not os.getenv('HF_TOKEN'):
|
| 11 |
+
st.error("Please set the HF_TOKEN environment variable")
|
| 12 |
+
st.stop()
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Select a model that works with the Inference API
|
| 15 |
+
model_repo = st.selectbox(
|
| 16 |
+
"Select Model",
|
| 17 |
+
[
|
| 18 |
+
"gpt2",
|
| 19 |
+
"distilgpt2",
|
| 20 |
+
"facebook/opt-350m",
|
| 21 |
+
"google/flan-t5-small"
|
| 22 |
+
],
|
| 23 |
+
index=1 # Default to distilgpt2 which usually works
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Initialize the HuggingFace model
|
| 27 |
+
try:
|
| 28 |
+
llm = HuggingFaceHub(
|
| 29 |
+
repo_id=model_repo,
|
| 30 |
+
huggingfacehub_api_token=os.getenv('HF_TOKEN'),
|
| 31 |
+
model_kwargs={
|
| 32 |
+
"max_length": 128,
|
| 33 |
+
"temperature": 0.7,
|
| 34 |
+
"max_new_tokens": 50
|
| 35 |
+
}
|
| 36 |
+
)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
st.error(f"Failed to initialize model: {str(e)}")
|
| 39 |
+
st.stop()
|
| 40 |
+
|
| 41 |
user_input = st.text_input("You:", "Type your message here...")
|
| 42 |
|
| 43 |
if st.button("Send"):
|
| 44 |
if user_input:
|
| 45 |
try:
|
| 46 |
+
response = llm(user_input)
|
| 47 |
+
st.write(f"Bot: {response}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
+
st.error(f"Error: {str(e)}. Try a different model or check your token.")
|
| 50 |
else:
|
| 51 |
st.write("Please enter a message.")
|