ilhamdev commited on
Commit
d0e7441
1 Parent(s): 6840ada

Rename app.py to index.js

Browse files
Files changed (2) hide show
  1. app.py +0 -63
  2. index.js +174 -0
app.py DELETED
@@ -1,63 +0,0 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
index.js ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const axios = require('axios');
3
+ const bodyParser = require('body-parser');
4
+ const fs = require('fs').promises;
5
+ const path = require('path');
6
+
7
+ const app = express();
8
+ const port = process.env.PORT || 3000;
9
+
10
+ app.use(bodyParser.json());
11
+
12
+ const sessionFilePath = path.join(__dirname, 'session.json');
13
+ const SESSION_TIMEOUT = 5 * 60 * 1000;
14
+ let sessions = {};
15
+
16
+ // Data untuk statistik
17
+ let totalRequests = 0;
18
+ let requestTimestamps = [];
19
+
20
+ async function loadSessions() {
21
+ try {
22
+ const data = await fs.readFile(sessionFilePath, 'utf-8');
23
+ sessions = JSON.parse(data);
24
+ } catch (error) {
25
+ if (error.code === 'ENOENT') {
26
+ sessions = {};
27
+ } else {
28
+ console.error('Error reading session file:', error);
29
+ }
30
+ }
31
+ }
32
+
33
+ async function saveSessions() {
34
+ try {
35
+ await fs.writeFile(sessionFilePath, JSON.stringify(sessions, null, 2), 'utf-8');
36
+ } catch (error) {
37
+ console.error('Error writing to session file:', error);
38
+ }
39
+ }
40
+
41
+ function cleanUpSessions() {
42
+ const now = Date.now();
43
+ for (const username in sessions) {
44
+ if (sessions[username].lastAccess && (now - sessions[username].lastAccess > SESSION_TIMEOUT)) {
45
+ delete sessions[username];
46
+ }
47
+ }
48
+ }
49
+
50
+ async function blackboxChat(content, username = null, prompt = null, webSearchMode = true) {
51
+ const url = "https://www.blackbox.ai/api/chat";
52
+ const headers = {
53
+ "Accept": "*/*",
54
+ "Accept-Language": "id-ID,en;q=0.5",
55
+ "Referer": "https://www.blackbox.ai/",
56
+ "Content-Type": "application/json",
57
+ "Origin": "https://www.blackbox.ai",
58
+ "Alt-Used": "www.blackbox.ai"
59
+ };
60
+
61
+ let messages;
62
+ if (username) {
63
+ cleanUpSessions();
64
+ if (!sessions[username]) {
65
+ sessions[username] = { messages: [], lastAccess: Date.now() };
66
+ } else {
67
+ sessions[username].lastAccess = Date.now();
68
+ }
69
+ sessions[username].messages.push({ role: "user", content });
70
+ messages = sessions[username].messages;
71
+ } else {
72
+ messages = [{ role: "user", content }];
73
+ }
74
+
75
+ const data = {
76
+ messages,
77
+ previewToken: null,
78
+ codeModelMode: true,
79
+ agentMode: {},
80
+ trendingAgentMode: {},
81
+ isMicMode: false,
82
+ userSystemPrompt: prompt || " and answers questions effectively. Keep a cool but supportive tone so users feel comfortable and appreciated. Be patient, empathetic and attentive to user needs, providing thoughtful and helpful responses. Make sure your way of speaking is indistinguishable from a human's, so that your presence feels natural and authentic. Use simple language, avoid technical jargon, remain consistent in providing solutions, and always be supportive and attentive. Show a humane attitude in every interaction and use Indonesian that is easy to understand.",
83
+ maxTokens: 1024,
84
+ webSearchMode: webSearchMode,
85
+ promptUrls: "",
86
+ isChromeExt: false,
87
+ githubToken: null
88
+ };
89
+
90
+ try {
91
+ const response = await axios.post(url, data, { headers });
92
+ let result = response.data;
93
+
94
+ // Existing replacement
95
+ result = result.replace(/\$@\$.*?\$@\$|v=.*?(\s|$)/g, '').trim(); // Remove content between $~~~$ markers
96
+ result = result.replace(/\$~~~\$.*?\$~~~\$/gs, '').trim();
97
+
98
+ const sourcesRegex = /Sources:.*?(\n|$)/s;
99
+ const sourcesMatch = result.match(sourcesRegex);
100
+ let sourcesText = "";
101
+
102
+ if (sourcesMatch) {
103
+ sourcesText = sourcesMatch[0].trim();
104
+ result = result.replace(sourcesRegex, '').trim();
105
+ }
106
+
107
+ if (sourcesText) {
108
+ result = `${result}\n\n${sourcesText}`;
109
+ }
110
+
111
+ if (username) {
112
+ sessions[username].messages.push({ role: "assistant", content: result });
113
+ await saveSessions();
114
+ }
115
+
116
+ return result;
117
+ } catch (error) {
118
+ console.error("Error fetching data:", error);
119
+ return "Maaf, terjadi kesalahan saat memproses permintaan Anda.";
120
+ }
121
+ }
122
+
123
+ loadSessions().catch(error => console.error("Failed to load sessions:", error));
124
+ setInterval(cleanUpSessions, SESSION_TIMEOUT);
125
+
126
+ // Middleware untuk melacak total permintaan dan RPS
127
+ app.use((req, res, next) => {
128
+ totalRequests++;
129
+ const now = Date.now();
130
+ requestTimestamps.push(now);
131
+ requestTimestamps = requestTimestamps.filter(timestamp => now - timestamp <= 1000);
132
+ next();
133
+ });
134
+
135
+ app.post('/', async (req, res) => {
136
+ const { content, user, prompt, webSearchMode } = req.body;
137
+ try {
138
+ const result = await blackboxChat(content, user, prompt, webSearchMode);
139
+ res.json({ result });
140
+ } catch (error) {
141
+ console.error("Error:", error.message);
142
+ res.status(400).json({ error: error.message });
143
+ }
144
+ });
145
+
146
+ app.get('/api/stats', (req, res) => {
147
+ const seconds = Number(process.uptime());
148
+ const d = Math.floor(seconds / (3600 * 24));
149
+ const h = Math.floor(seconds % (3600 * 24) / 3600);
150
+ const m = Math.floor(seconds % 3600 / 60);
151
+ const s = Math.floor(seconds % 60);
152
+
153
+ const dDisplay = d > 0 ? d + (d == 1 ? " Hari, " : " Hari, ") : "";
154
+ const hDisplay = h > 0 ? h + (h == 1 ? " Jam, " : " Jam, ") : "";
155
+ const mDisplay = m > 0 ? m + (m == 1 ? " Menit, " : " Menit, ") : "";
156
+ const sDisplay = s > 0 ? s + (s == 1 ? " Detik" : " Detik") : "";
157
+
158
+ const runtime = dDisplay + hDisplay + mDisplay + sDisplay;
159
+ const rps = requestTimestamps.length;
160
+
161
+ res.json({
162
+ rps: rps, // Requests Per Second
163
+ totalRequests: totalRequests, // Total Requests
164
+ runtime: runtime // Runtime in the desired format
165
+ });
166
+ });
167
+
168
+ app.use((req, res) => {
169
+ res.sendFile(path.join(__dirname, 'index.html'));
170
+ });
171
+
172
+ app.listen(7860, () => {
173
+ console.log(`Server is listening at http://localhost:${port}`);
174
+ });