File size: 1,309 Bytes
3702f14
47be670
9136861
3702f14
c40c931
782c755
 
c40c931
04868c4
782c755
3702f14
 
 
782c755
 
 
 
 
9136861
47be670
9136861
782c755
 
 
 
 
 
47be670
782c755
 
 
47be670
782c755
 
c40c931
 
782c755
c40c931
 
 
782c755
 
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
import streamlit as st
import requests
import os

# Load Hugging Face API token
HF_TOKEN = os.getenv("HF_TOKEN") or st.secrets["HF_TOKEN"]

# Use a free model that works
API_URL = "https://api-inference.huggingface.co/models/gpt2"
headers = {"Authorization": f"Bearer {HF_TOKEN}"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)

    # Debugging info
    st.write("🔍 Debug - Status Code:", response.status_code)
    st.write("🔍 Debug - Raw Response:", response.text)

    try:
        return response.json()
    except ValueError:
        return {"error": "Invalid JSON response from Hugging Face"}

# Streamlit UI
st.title("💡 Offline AI Chat App (HF Inference)")

user_input = st.text_area("Ask me something:")

if st.button("Send"):
    if user_input.strip():
        output = query({"inputs": user_input})

        if "error" in output:
            st.error(f"⚠️ Error: {output['error']}")
        elif isinstance(output, list) and "generated_text" in output[0]:
            # ✅ Extract clean text
            st.success("🤖 Reply:")
            st.write(output[0]["generated_text"])
        else:
            st.warning("⚠️ Unexpected response format. Check debug output above.")
    else:
        st.warning("Please enter a message first!")