Spaces:
Running
Running
import gradio as gr | |
from huggingface_hub import InferenceClient | |
import os | |
from dotenv import load_dotenv | |
load_dotenv() | |
api_url = os.getenv("API_URL") | |
if api_url is None: | |
raise ValueError("API_URL environment variable not set. Please create a .env file with API_URL.") | |
client = InferenceClient(api_url) | |
# FIXED_MAX_TOKENS, FIXED_TEMPERATURE, FIXED_TOP_P have been removed. | |
# SYSTEM_PROMPT has been entirely removed as requested. | |
def respond(message, history): | |
messages = [] # Removed the initial system prompt addition here | |
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: | |
for chunk in client.chat.completions.create( | |
messages=messages, | |
stream=True, | |
# max_tokens, temperature, and top_p are removed from here | |
): | |
if chunk.choices[0].delta.content is not None: | |
token = chunk.choices[0].delta.content | |
response_text += token | |
yield response_text | |
except Exception as e: | |
yield f"An error occurred: {e}" | |
# header_image_path has been removed. | |
kofi_script = """ | |
<script src='https://storage.ko-fi.com/cdn/scripts/overlay-widget.js'></script> | |
<script> | |
kofiWidgetOverlay.draw('sonnydesorbo', { | |
'type': 'floating-chat', | |
'floating-chat.donateButton.text': 'Support me', | |
'floating-chat.donateButton.background-color': '#00b9fe', | |
'floating-chat.donateButton.text-color': '#fff' | |
}); | |
</script> | |
""" | |
kofi_button_html = """ | |
<div style="text-align: center; padding: 20px;"> | |
<a href='https://ko-fi.com/Z8Z51E5TIG' target='_blank'> | |
<img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi5.png?v=6' border='0' alt='Buy Me a Coffee at ko-fi.com' /> | |
</a> | |
</div> | |
""" | |
donation_solicitation_html = """ | |
<div style="text-align: center; font-size: x-small; margin-bottom: 5px;"> | |
Xortron is truly uncensored, actually intelligent, and is provided to the world entirely for free. This app is unlimited, with no download necessary, no sign-up, and no log-in of any kind. I self-host the backend on my own personal hardware and the apps popularity is growing expensive. If this app has helped or entertained you please consider supporting @ ko-fi.com/xortron<br> | |
</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; } | |
""" | |
with gr.Blocks(theme="dark", head=kofi_script, css=custom_css) as demo: | |
# gr.Image component has been removed. | |
gr.ChatInterface( | |
fn=respond, | |
chatbot=gr.Chatbot( | |
height=800, | |
label="Xortron - Criminal Computing" | |
) | |
) | |
gr.HTML(donation_solicitation_html) | |
gr.HTML(kofi_button_html) | |
if __name__ == "__main__": | |
try: | |
demo.launch(show_api=False, share=True) | |
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}") |