Spaces:
Runtime error

MESC.Ltd / src /streamlit_app.py
Mleity's picture
Update src/streamlit_app.py
7ddd695 verified
Raw History Blame Contribute Delete
2.91 kB
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)