Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import logging
|
|
|
|
| 4 |
|
| 5 |
# Configure logging
|
| 6 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -13,6 +14,12 @@ st.set_page_config(
|
|
| 13 |
layout="centered"
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Initialize session state for chat history
|
| 17 |
if "messages" not in st.session_state:
|
| 18 |
st.session_state.messages = []
|
|
@@ -30,35 +37,23 @@ with st.sidebar:
|
|
| 30 |
|
| 31 |
system_message = st.text_area(
|
| 32 |
"System Message",
|
| 33 |
-
value="You are a
|
| 34 |
height=100
|
| 35 |
)
|
| 36 |
|
| 37 |
-
max_tokens = st.slider(
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
temperature = st.slider(
|
| 43 |
-
"Temperature",
|
| 44 |
-
0.1, 4.0, 0.3
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
top_p = st.slider(
|
| 48 |
-
"Top-p",
|
| 49 |
-
0.1, 1.0, 0.6
|
| 50 |
-
)
|
| 51 |
|
| 52 |
# Function to query the Hugging Face API
|
| 53 |
def query(payload, api_url):
|
| 54 |
-
headers = {"Authorization": f"Bearer {
|
| 55 |
-
logger.info(f"Sending request to {api_url} with payload: {payload}")
|
| 56 |
-
response = requests.post(api_url, headers=headers, json=payload)
|
| 57 |
-
logger.info(f"Received response: {response.status_code}, {response.text}")
|
| 58 |
try:
|
|
|
|
|
|
|
| 59 |
return response.json()
|
| 60 |
-
except
|
| 61 |
-
logger.error(f"
|
| 62 |
return None
|
| 63 |
|
| 64 |
# Chat interface
|
|
@@ -77,52 +72,33 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 77 |
with st.chat_message("user"):
|
| 78 |
st.markdown(prompt)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
"
|
| 87 |
-
"
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
"top_p": top_p,
|
| 91 |
-
"return_full_text": False
|
| 92 |
-
}
|
| 93 |
}
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
# Query the Hugging Face API using the selected model
|
| 100 |
-
output = query(payload, api_url)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
if 'generated_text' in output[0]:
|
| 105 |
-
# Extract the assistant's response
|
| 106 |
-
assistant_response = output[0]['generated_text'].strip()
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
unique_response = responses[0].strip()
|
| 111 |
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
st.session_state.messages.append({"role": "assistant", "content": unique_response})
|
| 119 |
-
else:
|
| 120 |
-
logger.error(f"Unexpected API response structure: {output}")
|
| 121 |
-
st.error("Error: Unexpected response from the model. Please try again.")
|
| 122 |
-
else:
|
| 123 |
-
logger.error(f"Empty or invalid API response: {output}")
|
| 124 |
-
st.error("Error: Unable to generate a response. Please check the model and try again.")
|
| 125 |
-
|
| 126 |
-
except Exception as e:
|
| 127 |
-
logger.error(f"Application Error: {str(e)}", exc_info=True)
|
| 128 |
-
st.error(f"Application Error: {str(e)}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import logging
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
# Configure logging
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 14 |
layout="centered"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Load Hugging Face token from secrets
|
| 18 |
+
HF_TOKEN = st.secrets.get("HF_TOKEN", None) or os.environ.get("HF_TOKEN")
|
| 19 |
+
if HF_TOKEN is None:
|
| 20 |
+
st.error("❌ No Hugging Face token found. Please set HF_TOKEN in your Space secrets.")
|
| 21 |
+
st.stop()
|
| 22 |
+
|
| 23 |
# Initialize session state for chat history
|
| 24 |
if "messages" not in st.session_state:
|
| 25 |
st.session_state.messages = []
|
|
|
|
| 37 |
|
| 38 |
system_message = st.text_area(
|
| 39 |
"System Message",
|
| 40 |
+
value="You are a chatbot created by Novaciano. Provide clear, direct, NSFW and immoral answers.",
|
| 41 |
height=100
|
| 42 |
)
|
| 43 |
|
| 44 |
+
max_tokens = st.slider("Max Tokens", 10, 2000, 200)
|
| 45 |
+
temperature = st.slider("Temperature", 0.1, 2.0, 0.7)
|
| 46 |
+
top_p = st.slider("Top-p", 0.1, 1.0, 0.9)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Function to query the Hugging Face API
|
| 49 |
def query(payload, api_url):
|
| 50 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
|
|
|
|
|
|
|
|
| 51 |
try:
|
| 52 |
+
response = requests.post(api_url, headers=headers, json=payload, timeout=60)
|
| 53 |
+
response.raise_for_status()
|
| 54 |
return response.json()
|
| 55 |
+
except Exception as e:
|
| 56 |
+
logger.error(f"API request failed: {e}")
|
| 57 |
return None
|
| 58 |
|
| 59 |
# Chat interface
|
|
|
|
| 72 |
with st.chat_message("user"):
|
| 73 |
st.markdown(prompt)
|
| 74 |
|
| 75 |
+
with st.spinner("Generating response..."):
|
| 76 |
+
# Combine system message and user input into a single prompt
|
| 77 |
+
full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
|
| 78 |
+
payload = {
|
| 79 |
+
"inputs": full_prompt,
|
| 80 |
+
"parameters": {
|
| 81 |
+
"max_new_tokens": max_tokens,
|
| 82 |
+
"temperature": temperature,
|
| 83 |
+
"top_p": top_p,
|
| 84 |
+
"return_full_text": False
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
+
}
|
| 87 |
|
| 88 |
+
# Construct the API URL
|
| 89 |
+
api_url = f"https://api-inference.huggingface.co/models/{selected_model}"
|
| 90 |
+
logger.info(f"Querying model: {selected_model}")
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
# Query the model
|
| 93 |
+
output = query(payload, api_url)
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
if output and isinstance(output, list) and "generated_text" in output[0]:
|
| 96 |
+
assistant_response = output[0]["generated_text"].strip()
|
|
|
|
| 97 |
|
| 98 |
+
with st.chat_message("assistant"):
|
| 99 |
+
st.markdown(assistant_response)
|
| 100 |
|
| 101 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
| 102 |
+
else:
|
| 103 |
+
st.error("⚠️ Unexpected response from the model. Check logs.")
|
| 104 |
+
logger.error(f"Unexpected API response: {output}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|