Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,143 +1,120 @@
|
|
1 |
-
from gradio_client import Client
|
2 |
import gradio as gr
|
3 |
-
import
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
"BICORP/GOGOGOGO",
|
8 |
-
"BICORP/server-2",
|
9 |
-
"BICORP/server-3",
|
10 |
-
"BICORP/server-4",
|
11 |
-
"BICORP/server-5",
|
12 |
-
"BICORP/server-6"
|
13 |
-
]
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
"Lake 1
|
18 |
-
"Lake
|
19 |
-
"Lake
|
20 |
-
|
21 |
-
]
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
def
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
try:
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
36 |
except Exception as e:
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
-
def
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
result = client.predict(
|
45 |
-
message,
|
46 |
-
model,
|
47 |
-
preset,
|
48 |
-
api_name="/chat"
|
49 |
-
)
|
50 |
-
return result
|
51 |
-
except Exception as e:
|
52 |
-
return "⚠️ Service unavailable. Please try your request again."
|
53 |
|
54 |
-
def
|
55 |
-
"""
|
56 |
-
|
57 |
-
|
58 |
-
"""
|
59 |
-
history
|
60 |
-
|
61 |
-
history.append((message, response))
|
62 |
-
return "", history
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
choices=MODELS,
|
73 |
-
value=MODELS[0],
|
74 |
-
interactive=True
|
75 |
-
)
|
76 |
-
preset_dropdown = gr.Dropdown(
|
77 |
-
label="⚙️ Performance Preset",
|
78 |
-
choices=PRESETS,
|
79 |
-
value=PRESETS[0],
|
80 |
-
interactive=True
|
81 |
-
)
|
82 |
-
model_info = gr.Markdown(
|
83 |
-
value=get_model_info(MODELS[0]),
|
84 |
-
label="📝 Model Specifications"
|
85 |
-
)
|
86 |
|
87 |
-
with gr.
|
88 |
-
|
89 |
-
|
90 |
-
height=
|
91 |
-
label="
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
134 |
|
135 |
-
|
136 |
-
demo.load(
|
137 |
-
fn=lambda: get_model_info(MODELS[0]),
|
138 |
-
outputs=model_info,
|
139 |
-
queue=False
|
140 |
-
)
|
141 |
|
142 |
if __name__ == "__main__":
|
143 |
-
demo
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
|
5 |
+
# --- API Configuration ---
|
6 |
+
BLACKBOX_URL = "https://api.blackbox.ai/api/chat"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# --- Model Configuration ---
|
9 |
+
api_models = {
|
10 |
+
"Lake 1 Mini": "mistralai/Mistral-Small-24B-Instruct-2501",
|
11 |
+
"Lake 1 Base": "databricks/dbrx-instruct",
|
12 |
+
"Lake 1 Chat": "deepseek-ai/deepseek-llm-67b-chat",
|
13 |
+
}
|
|
|
14 |
|
15 |
+
# --- Default Settings ---
|
16 |
+
DEFAULT_SETTINGS = {
|
17 |
+
'tpm': 600, # tokens per minute
|
18 |
+
'rpm': 7, # requests per minute
|
19 |
+
'preset': 'Normal'
|
20 |
+
}
|
21 |
|
22 |
+
def call_blackbox_api(prompt: str, model: str, max_new_tokens: int) -> str:
|
23 |
+
headers = {'Content-Type': 'application/json'}
|
24 |
+
payload = json.dumps({
|
25 |
+
"messages": [{"role": "user", "content": prompt}],
|
26 |
+
"model": model,
|
27 |
+
"max_tokens": str(max_new_tokens)
|
28 |
+
})
|
29 |
+
response = requests.post(BLACKBOX_URL, headers=headers, data=payload)
|
30 |
+
if response.status_code == 200 and "application/json" in response.headers.get('Content-Type', ''):
|
31 |
try:
|
32 |
+
data = response.json()
|
33 |
+
if 'choices' in data and data['choices']:
|
34 |
+
return data['choices'][0]['message']['content']
|
35 |
+
else:
|
36 |
+
return "Error: Unexpected response format."
|
37 |
except Exception as e:
|
38 |
+
return f"Error parsing JSON: {e}"
|
39 |
+
else:
|
40 |
+
return f"{response.text}"
|
41 |
|
42 |
+
def generate_response(message: str, model_name: str, preset: str) -> str:
|
43 |
+
max_tokens = DEFAULT_SETTINGS['tpm']
|
44 |
+
api_model = api_models[model_name]
|
45 |
+
return call_blackbox_api(message, model=api_model, max_new_tokens=max_tokens)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
def chat_handler(message, history, settings_state):
|
48 |
+
model = settings_state.get("model", "Lake 1 Mini")
|
49 |
+
preset = settings_state.get("preset", "Normal")
|
50 |
+
response = generate_response(message, model, preset)
|
51 |
+
history.append({"role": "user", "content": message})
|
52 |
+
history.append({"role": "assistant", "content": response})
|
53 |
+
return history
|
|
|
|
|
54 |
|
55 |
+
def update_settings(model, preset):
|
56 |
+
new_state = {"model": model, "preset": preset}
|
57 |
+
settings_text = f"Model: **{model}**, Preset: **{preset}**"
|
58 |
+
return new_state, settings_text
|
59 |
+
|
60 |
+
def create_interface():
|
61 |
+
with gr.Blocks(title="Lake AI Assistant", theme="soft") as demo:
|
62 |
+
settings_state = gr.State(value={"model": "Lake 1 Mini", "preset": "Normal"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
with gr.Tabs():
|
65 |
+
with gr.Tab("Chat"):
|
66 |
+
gr.Markdown("## Chat with Lake AI Assistant")
|
67 |
+
chatbot = gr.Chatbot(label="💬 Conversation", height=400, show_copy_button=True, type="messages")
|
68 |
+
chat_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", interactive=True)
|
69 |
+
send_button = gr.Button("Send")
|
70 |
+
donate_button = gr.Button("Donate ☕")
|
71 |
+
|
72 |
+
send_button.click(
|
73 |
+
fn=chat_handler,
|
74 |
+
inputs=[chat_input, chatbot, settings_state],
|
75 |
+
outputs=chatbot
|
76 |
+
)
|
77 |
+
chat_input.submit(
|
78 |
+
fn=chat_handler,
|
79 |
+
inputs=[chat_input, chatbot, settings_state],
|
80 |
+
outputs=chatbot
|
81 |
+
)
|
82 |
+
# Corrected donation button implementation
|
83 |
+
donate_button.click(
|
84 |
+
fn=None,
|
85 |
+
inputs=None,
|
86 |
+
outputs=None,
|
87 |
+
js="window.open('https://buymeacoffee.com/bronio_int', '_blank')"
|
88 |
+
)
|
89 |
+
|
90 |
+
with gr.Tab("Settings"):
|
91 |
+
gr.Markdown("## Settings")
|
92 |
+
with gr.Row():
|
93 |
+
with gr.Column():
|
94 |
+
gr.Markdown("### Model & Performance")
|
95 |
+
model_dropdown = gr.Dropdown(
|
96 |
+
label="Model Selection",
|
97 |
+
interactive=True,
|
98 |
+
choices=["Lake 1 Mini", "Lake 1 Base", "Lake 1 Chat"],
|
99 |
+
value="Lake 1 Mini"
|
100 |
+
)
|
101 |
+
preset_dropdown = gr.Dropdown(
|
102 |
+
label="Performance Preset",
|
103 |
+
interactive=True,
|
104 |
+
choices=["Fast", "Normal", "Quality"],
|
105 |
+
value="Normal"
|
106 |
+
)
|
107 |
+
update_settings_button = gr.Button("Update Settings")
|
108 |
+
settings_info = gr.Markdown("")
|
109 |
+
|
110 |
+
update_settings_button.click(
|
111 |
+
fn=update_settings,
|
112 |
+
inputs=[model_dropdown, preset_dropdown],
|
113 |
+
outputs=[settings_state, settings_info]
|
114 |
+
)
|
115 |
|
116 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
if __name__ == "__main__":
|
119 |
+
demo = create_interface()
|
120 |
+
demo.launch()
|