File size: 1,614 Bytes
ccabf63
be50a26
3a508f7
5b29361
be50a26
 
 
 
 
 
 
 
 
3cf6fd9
be50a26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import streamlit as st
import requests
import json

# Read the API token from secrets.toml
try:
    api_token = st.secrets["huggingface_api_token"]
except KeyError:
    st.error("Hugging Face API token not found in secrets.toml. Please add it.")
    st.stop() # Stop execution if token is missing


API_URL = "https://api-inference.huggingface.co/models/tencent/Tencent-Hunyuan-Large"
headers = {"Authorization": f"Bearer {HF_API_KEY}"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    try:
        return response.json()
    except requests.exceptions.RequestException as e:
        st.error(f"An error occurred: {e}")
        return None
    except json.JSONDecodeError as e:
        st.error(f"Invalid JSON response: {e}")
        return None


st.title("Tencent HunYuan Large Language Model")

user_input = st.text_area("Enter your text here:", height=150)

if st.button("Submit"):
    if not user_input:
        st.warning("Please enter some text.")
    else:
        with st.spinner("Generating response..."):
            output = query({"inputs": user_input})
            if output:
                try:
                    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)
                    st.success("Response:")
                    st.write(response_text)
                except (KeyError, IndexError, TypeError) as e:
                    st.error(f"Unexpected response format: {e}. Response: {output}")