Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
api_url = os.getenv("API_URL")
|
9 |
+
|
10 |
+
if api_url is None:
|
11 |
+
raise ValueError("API_URL environment variable not set. Please create a .env file with API_URL.")
|
12 |
+
|
13 |
+
client = InferenceClient(api_url)
|
14 |
+
FIXED_MAX_TOKENS = 2048
|
15 |
+
FIXED_TEMPERATURE = 0.9
|
16 |
+
FIXED_TOP_P = 0.95
|
17 |
+
|
18 |
+
# SYSTEM_PROMPT has been entirely removed as requested.
|
19 |
+
|
20 |
+
def respond(message, history):
|
21 |
+
messages = [] # Removed the initial system prompt addition here
|
22 |
+
|
23 |
+
for user_message, ai_message in history:
|
24 |
+
if user_message:
|
25 |
+
messages.append({"role": "user", "content": user_message})
|
26 |
+
if ai_message:
|
27 |
+
messages.append({"role": "assistant", "content": ai_message})
|
28 |
+
|
29 |
+
messages.append({"role": "user", "content": message})
|
30 |
+
|
31 |
+
response_text = ""
|
32 |
+
|
33 |
+
try:
|
34 |
+
for chunk in client.chat.completions.create(
|
35 |
+
messages=messages,
|
36 |
+
max_tokens=FIXED_MAX_TOKENS,
|
37 |
+
stream=True,
|
38 |
+
temperature=FIXED_TEMPERATURE,
|
39 |
+
top_p=FIXED_TOP_P,
|
40 |
+
):
|
41 |
+
if chunk.choices[0].delta.content is not None:
|
42 |
+
token = chunk.choices[0].delta.content
|
43 |
+
response_text += token
|
44 |
+
yield response_text
|
45 |
+
except Exception as e:
|
46 |
+
yield f"An error occurred: {e}"
|
47 |
+
|
48 |
+
header_image_path = "https://cdn-uploads.huggingface.co/production/uploads/6540a02d1389943fef4d2640/j61iZTDaK9g0UW3aWGwWi.gif"
|
49 |
+
kofi_script = """
|
50 |
+
<script src='https://storage.ko-fi.com/cdn/scripts/overlay-widget.js'></script>
|
51 |
+
<script>
|
52 |
+
kofiWidgetOverlay.draw('sonnydesorbo', {
|
53 |
+
'type': 'floating-chat',
|
54 |
+
'floating-chat.donateButton.text': 'Support me',
|
55 |
+
'floating-chat.donateButton.background-color': '#00b9fe',
|
56 |
+
'floating-chat.donateButton.text-color': '#fff'
|
57 |
+
});
|
58 |
+
</script>
|
59 |
+
"""
|
60 |
+
kofi_button_html = """
|
61 |
+
<div style="text-align: center; padding: 20px;">
|
62 |
+
<a href='https://ko-fi.com/Z8Z51E5TIG' target='_blank'>
|
63 |
+
<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' />
|
64 |
+
</a>
|
65 |
+
</div>
|
66 |
+
"""
|
67 |
+
donation_solicitation_html = """
|
68 |
+
<div style="text-align: center; font-size: x-small; margin-bottom: 5px;">
|
69 |
+
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>
|
70 |
+
|
71 |
+
</div>
|
72 |
+
"""
|
73 |
+
custom_css = """
|
74 |
+
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&display=swap');
|
75 |
+
body, .gradio-container {
|
76 |
+
font-family: 'Orbitron', sans-serif !important;
|
77 |
+
}
|
78 |
+
.gr-button { font-family: 'Orbitron', sans-serif !important; }
|
79 |
+
.gr-input { font-family: 'Orbitron', sans-serif !important; }
|
80 |
+
.gr-label { font-family: 'Orbitron', sans-serif !important; }
|
81 |
+
.gr-chatbot .message { font-family: 'Orbitron', sans-serif !important; }
|
82 |
+
"""
|
83 |
+
|
84 |
+
with gr.Blocks(theme="dark", head=kofi_script, css=custom_css) as demo:
|
85 |
+
gr.Image(
|
86 |
+
value=header_image_path,
|
87 |
+
label="Chatbot Header",
|
88 |
+
show_label=False,
|
89 |
+
interactive=False,
|
90 |
+
height=100,
|
91 |
+
elem_id="chatbot-logo"
|
92 |
+
)
|
93 |
+
|
94 |
+
gr.ChatInterface(
|
95 |
+
fn=respond,
|
96 |
+
chatbot=gr.Chatbot(
|
97 |
+
height=700,
|
98 |
+
label="Xortron Chat"
|
99 |
+
)
|
100 |
+
)
|
101 |
+
|
102 |
+
gr.HTML(donation_solicitation_html)
|
103 |
+
gr.HTML(kofi_button_html)
|
104 |
+
|
105 |
+
if __name__ == "__main__":
|
106 |
+
try:
|
107 |
+
demo.launch(show_api=False, share=True)
|
108 |
+
except NameError as ne:
|
109 |
+
print(f"Gradio demo could not be launched. 'client' might not have been initialized: {ne}")
|
110 |
+
except RuntimeError as re:
|
111 |
+
print(f"Gradio demo could not be launched due to an error during client initialization: {re}")
|
112 |
+
except Exception as e:
|
113 |
+
print(f"An unexpected error occurred when trying to launch Gradio demo: {e}")
|