import gradio as gr from openai import OpenAI import os from dotenv import load_dotenv load_dotenv() 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 = 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 (compatible with OpenAI's API), including a static system prompt. Args: message (str): The latest message from the user. history (list of lists): A list where each inner list contains a pair of [user_message, ai_message]. Yields: str: The generated response token by token (for streaming). """ messages = [{"role": "system", "content": SYSTEM_PROMPT}] for user_message, ai_message in history: if user_message: messages.append({"role": "user", "content": user_message}) if ai_message: messages.append({"role": "assistant", "content": ai_message}) messages.append({"role": "user", "content": message}) response_text = "" try: stream = client.chat.completions.create( messages=messages, model=FEATHERLESS_MODEL, stream=True, ) for chunk in stream: if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content is not None: token = chunk.choices[0].delta.content response_text += token yield response_text elif chunk.choices and chunk.choices[0].message and chunk.choices[0].message.content is not None: token = chunk.choices[0].message.content response_text += token 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 = """ """ kofi_button_html = """
""" donation_solicitation_html = """