BICORP commited on
Commit
9a7392a
·
verified ·
1 Parent(s): f44c33f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -129
app.py CHANGED
@@ -1,143 +1,120 @@
1
- from gradio_client import Client
2
  import gradio as gr
3
- import random
 
4
 
5
- # Hidden server configuration
6
- SERVERS = [
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
- MODELS = [
16
- "Lake 1 Base",
17
- "Lake 1 Advanced",
18
- "Lake 2 Chat [Closed Alpha]",
19
- "Lake 2 Base [Closed Beta]",
20
- "Lake 2 Pro [Planned]"
21
- ]
22
 
23
- PRESETS = ["Fast", "Normal", "Quality", "Unreal Performance"]
 
 
 
 
 
24
 
25
- def get_random_server():
26
- """Randomly select from available servers"""
27
- return random.choice(SERVERS)
28
-
29
- def get_model_info(model_name: str) -> str:
30
- """Fetch model specs with retry logic"""
31
- max_retries = 2
32
- for _ in range(max_retries):
 
33
  try:
34
- client = Client(get_random_server())
35
- return client.predict(model_name, api_name="/get_model_info")
 
 
 
36
  except Exception as e:
37
- continue
38
- return "⚠️ Failed to load specifications. Please try again later."
 
39
 
40
- def handle_chat(message: str, history, model: str, preset: str):
41
- """Process chat messages with automatic server selection"""
42
- try:
43
- client = Client(get_random_server())
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 respond(message, history, model, preset):
55
- """
56
- Append the user's message and model's response to the conversation history.
57
- Returns an empty string (to clear the input textbox) and the updated chat history.
58
- """
59
- history = history or []
60
- response = handle_chat(message, history, model, preset)
61
- history.append((message, response))
62
- return "", history
63
 
64
- with gr.Blocks(title="BI Corp AI Assistant", theme="soft") as demo:
65
- gr.Markdown("# <center>🏔️ BI Corp AI Assistant</center>")
66
- gr.Markdown("### <center>Enterprise-Grade AI Solutions</center>")
67
-
68
- with gr.Row():
69
- with gr.Column(scale=1):
70
- model_dropdown = gr.Dropdown(
71
- label="🤖 Model Selection",
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.Column(scale=3):
88
- # Reduced the height of the Chatbot to keep the textbox visible.
89
- chatbot = gr.Chatbot(
90
- height=300,
91
- label="💬 Conversation",
92
- show_copy_button=True
93
- )
94
- message_input = gr.Textbox(
95
- placeholder="Type your message...",
96
- container=True,
97
- scale=7,
98
- autofocus=True
99
- )
100
- send_button = gr.Button("🚀 Send", variant="primary")
101
-
102
- # Update the model specifications when a different model is selected.
103
- model_dropdown.change(
104
- fn=get_model_info,
105
- inputs=model_dropdown,
106
- outputs=model_info,
107
- queue=False
108
- )
109
-
110
- # Wire the Send button and Enter key in the Textbox to process chat messages.
111
- send_button.click(
112
- fn=respond,
113
- inputs=[message_input, chatbot, model_dropdown, preset_dropdown],
114
- outputs=[message_input, chatbot],
115
- queue=True
116
- )
117
-
118
- # Allow the Enter key in the textbox to trigger the same function.
119
- message_input.submit(
120
- fn=respond,
121
- inputs=[message_input, chatbot, model_dropdown, preset_dropdown],
122
- outputs=[message_input, chatbot],
123
- queue=True
124
- )
125
-
126
- # Clear history button to reset the conversation.
127
- clear_btn = gr.Button("🧹 Clear History")
128
- clear_btn.click(
129
- fn=lambda: ("", []),
130
- inputs=[],
131
- outputs=[message_input, chatbot],
132
- queue=False
133
- )
 
 
 
 
134
 
135
- # Initialize model specifications on app load.
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.launch()
 
 
 
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()