Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,22 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
-
import json
|
4 |
-
|
5 |
-
# Read the API token from secrets.toml
|
6 |
-
try:
|
7 |
-
api_token = st.secrets["huggingface_api_token"]
|
8 |
-
except KeyError:
|
9 |
-
st.error("Hugging Face API token not found in secrets.toml. Please add it.")
|
10 |
-
st.stop() # Stop execution if token is missing
|
11 |
-
|
12 |
|
|
|
13 |
API_URL = "https://api-inference.huggingface.co/models/tencent/Tencent-Hunyuan-Large"
|
14 |
-
|
15 |
|
16 |
def query(payload):
|
|
|
17 |
response = requests.post(API_URL, headers=headers, json=payload)
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
user_input = st.text_area("Enter your text here:", height=150)
|
31 |
-
|
32 |
-
if st.button("Submit"):
|
33 |
-
if not user_input:
|
34 |
-
st.warning("Please enter some text.")
|
35 |
-
else:
|
36 |
-
with st.spinner("Generating response..."):
|
37 |
-
output = query({"inputs": user_input})
|
38 |
-
if output:
|
39 |
-
try:
|
40 |
-
response_text = output[0]['generated_text'] if isinstance(output, list) and len(output) > 0 and 'generated_text' in output[0] else output["generated_text"] if "generated_text" in output else str(output)
|
41 |
-
st.success("Response:")
|
42 |
-
st.write(response_text)
|
43 |
-
except (KeyError, IndexError, TypeError) as e:
|
44 |
-
st.error(f"Unexpected response format: {e}. Response: {output}")
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Set up the API endpoint and headers
|
5 |
API_URL = "https://api-inference.huggingface.co/models/tencent/Tencent-Hunyuan-Large"
|
6 |
+
HF_API_KEY = st.secrets["HF_API_KEY"] # Load the API key from secrets
|
7 |
|
8 |
def query(payload):
|
9 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
10 |
response = requests.post(API_URL, headers=headers, json=payload)
|
11 |
+
return response.json()
|
12 |
+
|
13 |
+
# Create a text input field for the user
|
14 |
+
st.title("Tencent-Hunyuan-Large Model Demo")
|
15 |
+
user_input = st.text_input("Enter your text:", "")
|
16 |
+
|
17 |
+
# Create a button to trigger the API request
|
18 |
+
if st.button("Query Model"):
|
19 |
+
# Prepare the payload and send the request
|
20 |
+
payload = {"inputs": user_input}
|
21 |
+
response = query(payload)
|
22 |
+
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|