Spaces:
Runtime error
Runtime error
Rename app.js to app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253import gradio as grfrom transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
# Choose a lightweight, open modelmodel_name = "mistralai/Mistral-7B-Instruct-v0.2"
tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained( model_name, torch_dtype="auto", device_map="auto")
pipe = pipeline( "text-generation", model=model, tokenizer=tokenizer, max_new_tokens=256, do_sample=True, temperature=0.7, top_p=0.9)
def chat(history, message): # Build conversation text prompt = "" for user, bot in history: prompt += f"User: {user}\nAssistant: {bot}\n" prompt += f"User: {message}\nAssistant:"
output = pipe(prompt)[0]["generated_text"] reply = output.split("Assistant:")[-1].strip()
history.append((message, reply)) return history, ""
with gr.Blocks() as demo: gr.Markdown("# 🔥 My Chatbot") chatbot = gr.Chatbot() msg = gr.Textbox(label="Say something") clear = gr.Button("Clear chat")
state = gr.State([])
def respond(message, history): if history is None: history = [] return chat(history, message)
msg.submit(respond, [msg, chatbot], [chatbot, msg]) clear.click(lambda: ([], ""), None, [chatbot, msg])
demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192import gradio as grimport osfrom dotenv import load_dotenv
load_dotenv()
# --- LANGFUSE SETUP ---# We use the drop-in OpenAI client. This automatically traces all model calls.# We removed the @observe decorator from the function to prevent Gradio conflicts.try: from langfuse.openai import OpenAI print("✅ SUCCESS: Langfuse OpenAI client loaded.") LANGFUSE_ACTIVE = Trueexcept ImportError as e: print(f"⚠️ WARNING: Langfuse not found ({e}).") print("ℹ️ FALLBACK: Switching to standard OpenAI.") from openai import OpenAI LANGFUSE_ACTIVE = False# ----------------------
SYSTEM_PROMPT = os.getenv("XTRNPMT")
API_BASE_URL = "https://api.featherless.ai/v1"
FEATHERLESS_API_KEY = os.getenv("FEATHERLESS_API_KEY")
FEATHERLESS_MODEL = "darkc0de/XortronCriminalComputingConfig"
if not FEATHERLESS_API_KEY: print("WARNING: FEATHERLESS_API_KEY environment variable is not set.")
try: if not FEATHERLESS_API_KEY: raise ValueError("FEATHERLESS_API_KEY is not set. Please set it as an environment variable or a secret in your deployment environment.")
# Client initialization # If Langfuse is active, this client automatically logs to Langfuse. client = OpenAI( base_url=API_BASE_URL, api_key=FEATHERLESS_API_KEY ) print(f"OpenAI client initialized with base_url: {API_BASE_URL} for Featherless AI, model: {FEATHERLESS_MODEL}")
except Exception as e: print(f"Error initializing OpenAI client with base_url '{API_BASE_URL}': {e}") raise RuntimeError( "Could not initialize OpenAI client. " f"Please check the API base URL ('{API_BASE_URL}'), your Featherless AI API key, model ID, " f"and ensure the server is accessible. Original error: {e}" )
def respond(message, history): """ This function processes the user's message and the chat history to generate a response from the language model using the Featherless AI API. """ # 32k tokens is roughly 128,000 characters. # We cap the context at 100,000 characters (~25k tokens) to leave 7k tokens safely for the AI's response generation. MAX_CONTEXT_CHARS = 100000 messages = [{"role": "system", "content": SYSTEM_PROMPT or ""}]
# 1. Calculate how many characters we have available for the chat history system_chars = len(SYSTEM_PROMPT or "") message_chars = len(message or "") allowed_history_chars = MAX_CONTEXT_CHARS - system_chars - message_chars
# 2. Iterate backwards through history to only keep the most recent messages that fit recent_history = [] current_hist_chars = 0 # In Gradio 6.0, history is a list of dicts: [{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}] for msg in reversed(history): content = msg.get("content", "") or "" role = msg.get("role", "user") turn_chars = len(content) # Truncate older messages if appending them exceeds our safe limit if current_hist_chars + turn_chars > allowed_history_chars: break recent_history.insert(0, {"role": role, "content": content}) current_hist_chars += turn_chars
# 3. Append the filtered history and the newest user message messages.extend(recent_history) messages.append({"role": "user", "content": message})
response_text = ""
try: # Optional: Add a name to the trace if Langfuse is active kwargs = {} if LANGFUSE_ACTIVE: kwargs["name"] = "featherless-generation"
stream = client.chat.completions.create( messages=messages, model=FEATHERLESS_MODEL, temperature=0.7, # Changed to 0.85 top_p=0.95, # Set top_p to 0.95 frequency_penalty=0.1, presence_penalty=0, stream=True, **kwargs )
for chunk in stream: # Check if there are choices and if the delta has content if chunk.choices and len(chunk.choices) > 0: delta = chunk.choices[0].delta if hasattr(delta, "content") and delta.content is not None: response_text += delta.content yield response_text
except Exception as e: error_message = f"An error occurred during model inference with Featherless AI: {e}" print(error_message) yield error_message
kofi_script = """<script src='https://storage.ko-fi.com/cdn/scripts/overlay-widget.js'></script><script> kofiWidgetOverlay.draw('xortron', { 'type': 'floating-chat', 'floating-chat.donateButton.text': 'Support me', 'floating-chat.donateButton.background-color': '#794bc4', 'floating-chat.donateButton.text-color': '#fff' });</script>"""
# Changed width of the image to 50%footer_image_html = """<div style="width: 100%; text-align: center; margin-top: 10px;"> <a href="https://ko-fi.com/Z8Z51E5TIG" target="_blank" rel="noopener noreferrer"> <img src="https://huggingface.co/spaces/xortron/chat/resolve/main/HiQrS.gif" alt="Support Xortron on Ko-fi" style="width: 70%; height: auto; display: block; border: none; margin: 0 auto;"> </a></div>"""
custom_css = """@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&display=swap');body, .gradio-container { font-family: 'Orbitron', sans-serif !important;}.gr-button { font-family: 'Orbitron', sans-serif !important; }.gr-input { font-family: 'Orbitron', sans-serif !important; }.gr-label { font-family: 'Orbitron', sans-serif !important; }.gr-chatbot .message { font-family: 'Orbitron', sans-serif !important; }
/* --- HIDE THE HUGGING FACE SPACES HEADER --- */#huggingface-spaces-header, #spaces-header, spaces-header, .spaces-header { display: none !important;}"""
with gr.Blocks(title="XORTRON") as demo: gr.ChatInterface( fn=respond, # The function to call when a message is sent chatbot=gr.Chatbot( # Configure the chatbot display area height=800, # Set the height of the chat history display to 800px label="XORTRON - Criminal Computing" # Set the label ) )
# Added the clickable header image below the chat window gr.HTML(footer_image_html)
if __name__ == "__main__": if not FEATHERLESS_API_KEY: print("\nCRITICAL ERROR: FEATHERLESS_API_KEY is not set.") print("Please ensure it's set as a secret in your Hugging Face Space settings or as an environment variable.\n")
try: demo.queue(default_concurrency_limit=2)
demo.launch(share=False, theme="Nymbo/Nymbo_Theme", head=kofi_script, css=custom_css) except NameError as ne: print(f"Gradio demo could not be launched. 'client' might not have been initialized: {ne}") except RuntimeError as re: print(f"Gradio demo could not be launched due to an error during client initialization: {re}") except Exception as e: print(f"An unexpected error occurred when trying to launch Gradio demo: {e}")app.jsDeleted-53
app.pyAdded+192