Spaces:
Runtime error

File size: 2,911 Bytes
ac3f175
7ddd695
79a3ab5
7ddd695
79a3ab5
d2d0cb6
7ddd695
79a3ab5
7ddd695
79a3ab5
 
 
 
50c7e23
79a3ab5
50c7e23
7ddd695
79a3ab5
7ddd695
 
 
 
 
 
 
 
79a3ab5
5d806ab
50c7e23
5d806ab
50c7e23
79a3ab5
7ddd695
79a3ab5
7ddd695
 
 
 
 
 
 
 
79a3ab5
7ddd695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d806ab
50c7e23
79a3ab5
f8f5259
5182024
 
 
b674abe
6fc0499
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
import requests
import os
import time

# --- TOOL CONFIGURATION ---
st.set_page_config(page_title="Global AI Tool", layout="centered")

# Retrieve Secret Key
token = os.environ.get("HF_TOKEN")

# --- HEADER & LOGO ---
if os.path.exists("logo.png"):
    st.image("logo.png", width=150)

st.markdown('<h1>🌐 Global AI Assistant</h1>', unsafe_allow_html=True)
st.markdown(f'<h3>Created by <span style="color:red;">[YOUR NAME]</span></h3>', unsafe_allow_html=True)

# --- THE STABLE BRAIN (Mistral v0.3) ---
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
headers = {"Authorization": f"Bearer {token}"}

def query(payload):
    # This sends the request and waits for a response
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

# --- USER INTERFACE ---
user_input = st.text_area("How can I help you?", placeholder="Type here...")

if st.button("Generate Result"):
    if not token:
        st.error("🚨 HF_TOKEN IS MISSING! Go to Settings -> Secrets and add 'HF_TOKEN'.")
    elif user_input:
        with st.spinner("Connecting to AI... please wait."):
            # We try up to 3 times if it's just "warming up"
            success = False
            for attempt in range(3):
                output = query({
                    "inputs": f"User: {user_input}\nAssistant:",
                    "parameters": {"max_new_tokens": 500, "wait_for_model": True}
                })
                
                # CHECK THE RESULT
                if isinstance(output, list) and len(output) > 0:
                    result = output[0].get('generated_text', "No text generated.")
                    # Clean up the response to show only the AI's part
                    clean_result = result.split("Assistant:")[1] if "Assistant:" in result else result
                    st.success(clean_result)
                    success = True
                    break
                elif "error" in output:
                    error_msg = output["error"]
                    if "currently loading" in error_msg.lower():
                        st.info(f"AI is loading (Attempt {attempt+1}/3). Waiting 10 seconds...")
                        time.sleep(10)
                    else:
                        st.error(f"AI Error: {error_msg}")
                        break
                else:
                    st.error(f"Unexpected result: {output}")
                    break
    else:
        st.warning("Please enter some text.")


st.caption("Powered by www.mesc.ltd")
st.caption("+2033950347 / 8")
st.caption("+201007590100")
st.markdown('<h4><style="color:red;"> قسم إدارة الأعمال </h4>', unsafe_allow_html=True,)
st.markdown('<h6>Powered by <H1 style="color:red;"> ©المحمدون </h6>للهندسة والخدمات والكيماويات ش.ذ.م.م', unsafe_allow_html=True)