Dmtlant commited on
Commit
929cdfc
·
verified ·
1 Parent(s): 3cf6fd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -37
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
- headers = {"Authorization": f"Bearer {HF_API_KEY}"}
15
 
16
  def query(payload):
 
17
  response = requests.post(API_URL, headers=headers, json=payload)
18
- try:
19
- return response.json()
20
- except requests.exceptions.RequestException as e:
21
- st.error(f"An error occurred: {e}")
22
- return None
23
- except json.JSONDecodeError as e:
24
- st.error(f"Invalid JSON response: {e}")
25
- return None
26
-
27
-
28
- st.title("Tencent HunYuan Large Language Model")
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)