Karmastudios commited on
Commit
75a2e42
·
verified ·
1 Parent(s): bd5713e

Rename app.js to app.py

Browse files
Files changed (2) hide show
  1. app.js +0 -53
  2. app.py +192 -0
app.js DELETED
@@ -1,53 +0,0 @@
1
- import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
-
4
- # Choose a lightweight, open model
5
- model_name = "mistralai/Mistral-7B-Instruct-v0.2"
6
-
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(
9
- model_name,
10
- torch_dtype="auto",
11
- device_map="auto"
12
- )
13
-
14
- pipe = pipeline(
15
- "text-generation",
16
- model=model,
17
- tokenizer=tokenizer,
18
- max_new_tokens=256,
19
- do_sample=True,
20
- temperature=0.7,
21
- top_p=0.9
22
- )
23
-
24
- def chat(history, message):
25
- # Build conversation text
26
- prompt = ""
27
- for user, bot in history:
28
- prompt += f"User: {user}\nAssistant: {bot}\n"
29
- prompt += f"User: {message}\nAssistant:"
30
-
31
- output = pipe(prompt)[0]["generated_text"]
32
- reply = output.split("Assistant:")[-1].strip()
33
-
34
- history.append((message, reply))
35
- return history, ""
36
-
37
- with gr.Blocks() as demo:
38
- gr.Markdown("# 🔥 My Chatbot")
39
- chatbot = gr.Chatbot()
40
- msg = gr.Textbox(label="Say something")
41
- clear = gr.Button("Clear chat")
42
-
43
- state = gr.State([])
44
-
45
- def respond(message, history):
46
- if history is None:
47
- history = []
48
- return chat(history, message)
49
-
50
- msg.submit(respond, [msg, chatbot], [chatbot, msg])
51
- clear.click(lambda: ([], ""), None, [chatbot, msg])
52
-
53
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from dotenv import load_dotenv
4
+
5
+ load_dotenv()
6
+
7
+ # --- LANGFUSE SETUP ---
8
+ # We use the drop-in OpenAI client. This automatically traces all model calls.
9
+ # We removed the @observe decorator from the function to prevent Gradio conflicts.
10
+ try:
11
+ from langfuse.openai import OpenAI
12
+ print("✅ SUCCESS: Langfuse OpenAI client loaded.")
13
+ LANGFUSE_ACTIVE = True
14
+ except ImportError as e:
15
+ print(f"⚠️ WARNING: Langfuse not found ({e}).")
16
+ print("ℹ️ FALLBACK: Switching to standard OpenAI.")
17
+ from openai import OpenAI
18
+ LANGFUSE_ACTIVE = False
19
+ # ----------------------
20
+
21
+ SYSTEM_PROMPT = os.getenv("XTRNPMT")
22
+
23
+ API_BASE_URL = "https://api.featherless.ai/v1"
24
+
25
+ FEATHERLESS_API_KEY = os.getenv("FEATHERLESS_API_KEY")
26
+
27
+ FEATHERLESS_MODEL = "darkc0de/XortronCriminalComputingConfig"
28
+
29
+ if not FEATHERLESS_API_KEY:
30
+ print("WARNING: FEATHERLESS_API_KEY environment variable is not set.")
31
+
32
+ try:
33
+ if not FEATHERLESS_API_KEY:
34
+ raise ValueError("FEATHERLESS_API_KEY is not set. Please set it as an environment variable or a secret in your deployment environment.")
35
+
36
+ # Client initialization
37
+ # If Langfuse is active, this client automatically logs to Langfuse.
38
+ client = OpenAI(
39
+ base_url=API_BASE_URL,
40
+ api_key=FEATHERLESS_API_KEY
41
+ )
42
+ print(f"OpenAI client initialized with base_url: {API_BASE_URL} for Featherless AI, model: {FEATHERLESS_MODEL}")
43
+
44
+ except Exception as e:
45
+ print(f"Error initializing OpenAI client with base_url '{API_BASE_URL}': {e}")
46
+ raise RuntimeError(
47
+ "Could not initialize OpenAI client. "
48
+ f"Please check the API base URL ('{API_BASE_URL}'), your Featherless AI API key, model ID, "
49
+ f"and ensure the server is accessible. Original error: {e}"
50
+ )
51
+
52
+
53
+ def respond(message, history):
54
+ """
55
+ This function processes the user's message and the chat history to generate a response
56
+ from the language model using the Featherless AI API.
57
+ """
58
+ # 32k tokens is roughly 128,000 characters.
59
+ # We cap the context at 100,000 characters (~25k tokens) to leave 7k tokens safely for the AI's response generation.
60
+ MAX_CONTEXT_CHARS = 100000
61
+
62
+ messages = [{"role": "system", "content": SYSTEM_PROMPT or ""}]
63
+
64
+ # 1. Calculate how many characters we have available for the chat history
65
+ system_chars = len(SYSTEM_PROMPT or "")
66
+ message_chars = len(message or "")
67
+ allowed_history_chars = MAX_CONTEXT_CHARS - system_chars - message_chars
68
+
69
+ # 2. Iterate backwards through history to only keep the most recent messages that fit
70
+ recent_history = []
71
+ current_hist_chars = 0
72
+
73
+ # In Gradio 6.0, history is a list of dicts: [{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]
74
+ for msg in reversed(history):
75
+ content = msg.get("content", "") or ""
76
+ role = msg.get("role", "user")
77
+
78
+ turn_chars = len(content)
79
+
80
+ # Truncate older messages if appending them exceeds our safe limit
81
+ if current_hist_chars + turn_chars > allowed_history_chars:
82
+ break
83
+
84
+ recent_history.insert(0, {"role": role, "content": content})
85
+ current_hist_chars += turn_chars
86
+
87
+ # 3. Append the filtered history and the newest user message
88
+ messages.extend(recent_history)
89
+ messages.append({"role": "user", "content": message})
90
+
91
+ response_text = ""
92
+
93
+ try:
94
+ # Optional: Add a name to the trace if Langfuse is active
95
+ kwargs = {}
96
+ if LANGFUSE_ACTIVE:
97
+ kwargs["name"] = "featherless-generation"
98
+
99
+ stream = client.chat.completions.create(
100
+ messages=messages,
101
+ model=FEATHERLESS_MODEL,
102
+ temperature=0.7, # Changed to 0.85
103
+ top_p=0.95, # Set top_p to 0.95
104
+ frequency_penalty=0.1,
105
+ presence_penalty=0,
106
+ stream=True,
107
+ **kwargs
108
+ )
109
+
110
+ for chunk in stream:
111
+ # Check if there are choices and if the delta has content
112
+ if chunk.choices and len(chunk.choices) > 0:
113
+ delta = chunk.choices[0].delta
114
+ if hasattr(delta, "content") and delta.content is not None:
115
+ response_text += delta.content
116
+ yield response_text
117
+
118
+ except Exception as e:
119
+ error_message = f"An error occurred during model inference with Featherless AI: {e}"
120
+ print(error_message)
121
+ yield error_message
122
+
123
+
124
+ kofi_script = """
125
+ <script src='https://storage.ko-fi.com/cdn/scripts/overlay-widget.js'></script>
126
+ <script>
127
+ kofiWidgetOverlay.draw('xortron', {
128
+ 'type': 'floating-chat',
129
+ 'floating-chat.donateButton.text': 'Support me',
130
+ 'floating-chat.donateButton.background-color': '#794bc4',
131
+ 'floating-chat.donateButton.text-color': '#fff'
132
+ });
133
+ </script>
134
+ """
135
+
136
+ # Changed width of the image to 50%
137
+ footer_image_html = """
138
+ <div style="width: 100%; text-align: center; margin-top: 10px;">
139
+ <a href="https://ko-fi.com/Z8Z51E5TIG" target="_blank" rel="noopener noreferrer">
140
+ <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;">
141
+ </a>
142
+ </div>
143
+ """
144
+
145
+ custom_css = """
146
+ @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&display=swap');
147
+ body, .gradio-container {
148
+ font-family: 'Orbitron', sans-serif !important;
149
+ }
150
+ .gr-button { font-family: 'Orbitron', sans-serif !important; }
151
+ .gr-input { font-family: 'Orbitron', sans-serif !important; }
152
+ .gr-label { font-family: 'Orbitron', sans-serif !important; }
153
+ .gr-chatbot .message { font-family: 'Orbitron', sans-serif !important; }
154
+
155
+ /* --- HIDE THE HUGGING FACE SPACES HEADER --- */
156
+ #huggingface-spaces-header,
157
+ #spaces-header,
158
+ spaces-header,
159
+ .spaces-header {
160
+ display: none !important;
161
+ }
162
+ """
163
+
164
+ with gr.Blocks(title="XORTRON") as demo:
165
+
166
+ gr.ChatInterface(
167
+ fn=respond, # The function to call when a message is sent
168
+ chatbot=gr.Chatbot( # Configure the chatbot display area
169
+ height=800, # Set the height of the chat history display to 800px
170
+ label="XORTRON - Criminal Computing" # Set the label
171
+ )
172
+ )
173
+
174
+ # Added the clickable header image below the chat window
175
+ gr.HTML(footer_image_html)
176
+
177
+
178
+ if __name__ == "__main__":
179
+ if not FEATHERLESS_API_KEY:
180
+ print("\nCRITICAL ERROR: FEATHERLESS_API_KEY is not set.")
181
+ print("Please ensure it's set as a secret in your Hugging Face Space settings or as an environment variable.\n")
182
+
183
+ try:
184
+ demo.queue(default_concurrency_limit=2)
185
+
186
+ demo.launch(share=False, theme="Nymbo/Nymbo_Theme", head=kofi_script, css=custom_css)
187
+ except NameError as ne:
188
+ print(f"Gradio demo could not be launched. 'client' might not have been initialized: {ne}")
189
+ except RuntimeError as re:
190
+ print(f"Gradio demo could not be launched due to an error during client initialization: {re}")
191
+ except Exception as e:
192
+ print(f"An unexpected error occurred when trying to launch Gradio demo: {e}")