Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,31 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
-
from ultralytics import YOLO
|
4 |
from PIL import Image, ImageDraw
|
5 |
import torch
|
|
|
6 |
|
7 |
-
# Load model YOLOv8
|
8 |
-
model = YOLO("yolov8n.pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Global chat history
|
11 |
chat_history = []
|
@@ -13,7 +33,8 @@ chat_history = []
|
|
13 |
# Fungsi untuk chatting dengan chatbot
|
14 |
def chat_with_bot(message):
|
15 |
global chat_history
|
16 |
-
|
|
|
17 |
chat_history.append((message, bot_response))
|
18 |
return chat_history
|
19 |
|
|
|
1 |
import os
|
2 |
+
import json
|
3 |
import gradio as gr
|
4 |
+
from ultralytics import YOLO
|
5 |
from PIL import Image, ImageDraw
|
6 |
import torch
|
7 |
+
from langchain.chat_models import ChatGoogleGenerativeAI
|
8 |
|
9 |
+
# Load model YOLOv8
|
10 |
+
model = YOLO("yolov8n.pt")
|
11 |
+
|
12 |
+
# Load credentials (stringified JSON) from environment variable for Gemini
|
13 |
+
credentials_string = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
|
14 |
+
if not credentials_string:
|
15 |
+
raise ValueError("GOOGLE_APPLICATION_CREDENTIALS is not set in the environment!")
|
16 |
+
|
17 |
+
# Parse the stringified JSON back to a Python dictionary
|
18 |
+
credentials = json.loads(credentials_string)
|
19 |
+
|
20 |
+
# Save the credentials to a temporary JSON file (required by Google SDKs)
|
21 |
+
with open("service_account.json", "w") as f:
|
22 |
+
json.dump(credentials, f)
|
23 |
+
|
24 |
+
# Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the temporary file
|
25 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service_account.json"
|
26 |
+
|
27 |
+
# Initialize Gemini model (chatbot)
|
28 |
+
llm = ChatGoogleGenerativeAI(model='gemini-1.5-pro')
|
29 |
|
30 |
# Global chat history
|
31 |
chat_history = []
|
|
|
33 |
# Fungsi untuk chatting dengan chatbot
|
34 |
def chat_with_bot(message):
|
35 |
global chat_history
|
36 |
+
response = llm.predict(message) # Menggunakan Gemini untuk menghasilkan respon
|
37 |
+
bot_response = f"Bot: {response}"
|
38 |
chat_history.append((message, bot_response))
|
39 |
return chat_history
|
40 |
|