ilhamap commited on
Commit
29a3b5e
·
verified ·
1 Parent(s): 1160abb

Upload 2 files

Browse files
Files changed (2) hide show
  1. index.mjs +75 -0
  2. package.json +22 -0
index.mjs ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import express from "express";
2
+ import 'dotenv/config.js'
3
+ import { createServer } from "http";
4
+ import { Server } from "socket.io";
5
+ import { GoogleGenerativeAI } from "@google/generative-ai";
6
+
7
+
8
+ const app = express();
9
+ const httpServer = createServer(app);
10
+ app.use(express.static('public'));
11
+ const io = new Server(httpServer, { /* options */ });
12
+
13
+ io.on("connection", (socket) => {
14
+
15
+ socket.on("ask_api", (client_data) => {
16
+ console.log(client_data)
17
+ console.log("trying to reach api");
18
+ asyncAPICall(client_data, socket)
19
+ });
20
+
21
+ });
22
+
23
+ // Access your API key as an environment variable (see "Set up your API key" above)
24
+ const genAI = new GoogleGenerativeAI(process.env.API_KEY);
25
+
26
+ async function run() {
27
+ // For text-only input, use the gemini-pro model
28
+ const model = genAI.getGenerativeModel({ model: "gemini-pro"});
29
+
30
+ const prompt = "Write a story about a magic backpack."
31
+
32
+ const result = await model.generateContent(prompt);
33
+ const response = await result.response;
34
+ const text = response.text();
35
+ console.log(text);
36
+ }
37
+
38
+ //run();
39
+ function fileToGenerativePart(path, mimeType) {
40
+ return {
41
+ inlineData: {
42
+ data: path,
43
+ mimeType
44
+ },
45
+ };
46
+ }
47
+ async function asyncAPICall(data, socket) {
48
+ try{
49
+ // For text-only input, use the gemini-pro model
50
+ const model = genAI.getGenerativeModel({ model: "gemini-pro-vision"});
51
+ const prompt = data[1]
52
+ const imageParts = [
53
+ fileToGenerativePart(data[0].slice(22), "image/png"),
54
+ ];
55
+ const result = await model.generateContent([prompt, ...imageParts]);
56
+ const response = await result.response;
57
+ const text = response.text();
58
+ socket.emit("api_response", (text))
59
+ }
60
+ catch(e){
61
+ console.log(e)
62
+ socket.emit("api_error", ("ERROR ON API SIDE, SORRY..."))
63
+ }
64
+
65
+ }
66
+
67
+
68
+
69
+ httpServer.listen(7860);
70
+ console.log("App running on http://localhost:7860")
71
+
72
+
73
+
74
+
75
+
package.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "live-vision",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.mjs",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1",
9
+ "server": "node index.js"
10
+ },
11
+ "author": "ilham",
12
+ "license": "MIT",
13
+ "dependencies": {
14
+ "@google/generative-ai": "^0.2.0",
15
+ "@gradio/client": "^0.10.1",
16
+ "dotenv": "^4.0.0",
17
+ "eventsource": "^2.0.2",
18
+ "express": "^4.18.2",
19
+ "fs": "^0.0.1-security",
20
+ "socket.io": "^4.5.4"
21
+ }
22
+ }