Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
from transformers import
|
4 |
-
from langchain_google_genai.chat_models import ChatGoogleGenerativeAI #
|
5 |
from PIL import Image
|
6 |
import torch
|
7 |
import json
|
8 |
import requests
|
9 |
|
10 |
-
# Load credentials (stringified JSON) from environment variable
|
11 |
credentials_string = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
|
12 |
if not credentials_string:
|
13 |
raise ValueError("GOOGLE_APPLICATION_CREDENTIALS is not set in the environment!")
|
@@ -22,23 +22,37 @@ with open("service_account.json", "w") as f:
|
|
22 |
# Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the temporary file
|
23 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service_account.json"
|
24 |
|
25 |
-
# Initialize
|
26 |
llm = ChatGoogleGenerativeAI(model='gemini-1.5-pro')
|
27 |
|
28 |
-
#
|
29 |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
30 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
31 |
|
32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
chat_history = []
|
34 |
|
|
|
35 |
def chat_with_gemini(message):
|
36 |
global chat_history
|
37 |
-
# Get a response from the Gemini model
|
38 |
bot_response = llm.predict(message) # This will interact with the Gemini model
|
39 |
chat_history.append((message, bot_response))
|
40 |
return chat_history
|
41 |
|
|
|
42 |
def analyze_image(image_path):
|
43 |
global chat_history
|
44 |
try:
|
@@ -73,7 +87,6 @@ def analyze_image(image_path):
|
|
73 |
chat_history.append(("Error during image analysis", error_msg))
|
74 |
return chat_history
|
75 |
|
76 |
-
|
77 |
# Build the Gradio interface
|
78 |
with gr.Blocks() as demo:
|
79 |
gr.Markdown("# Ken Chatbot")
|
@@ -128,5 +141,5 @@ with gr.Blocks() as demo:
|
|
128 |
</style>
|
129 |
""")
|
130 |
|
131 |
-
# Launch
|
132 |
demo.launch()
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
4 |
+
from langchain_google_genai.chat_models import ChatGoogleGenerativeAI # Import Gemini
|
5 |
from PIL import Image
|
6 |
import torch
|
7 |
import json
|
8 |
import requests
|
9 |
|
10 |
+
# Load credentials (stringified JSON) from environment variable for Gemini
|
11 |
credentials_string = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
|
12 |
if not credentials_string:
|
13 |
raise ValueError("GOOGLE_APPLICATION_CREDENTIALS is not set in the environment!")
|
|
|
22 |
# Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the temporary file
|
23 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service_account.json"
|
24 |
|
25 |
+
# Initialize Gemini model (chatbot)
|
26 |
llm = ChatGoogleGenerativeAI(model='gemini-1.5-pro')
|
27 |
|
28 |
+
# Initialize DETR model and processor for object detection
|
29 |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
30 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
31 |
|
32 |
+
# Load COCO class labels (from the official COCO dataset)
|
33 |
+
COCO_CLASSES = [
|
34 |
+
'N/A', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat',
|
35 |
+
'traffic light', 'fire hydrant', 'N/A', 'stop sign', 'parking meter', 'bench', 'bird', 'cat',
|
36 |
+
'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella',
|
37 |
+
'N/A', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat',
|
38 |
+
'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'N/A', 'wine glass', 'cup',
|
39 |
+
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot',
|
40 |
+
'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet',
|
41 |
+
'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
|
42 |
+
'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
|
43 |
+
]
|
44 |
+
|
45 |
+
# Global chat history variable
|
46 |
chat_history = []
|
47 |
|
48 |
+
# Function for chatting with Gemini
|
49 |
def chat_with_gemini(message):
|
50 |
global chat_history
|
|
|
51 |
bot_response = llm.predict(message) # This will interact with the Gemini model
|
52 |
chat_history.append((message, bot_response))
|
53 |
return chat_history
|
54 |
|
55 |
+
# Function for analyzing the uploaded image
|
56 |
def analyze_image(image_path):
|
57 |
global chat_history
|
58 |
try:
|
|
|
87 |
chat_history.append(("Error during image analysis", error_msg))
|
88 |
return chat_history
|
89 |
|
|
|
90 |
# Build the Gradio interface
|
91 |
with gr.Blocks() as demo:
|
92 |
gr.Markdown("# Ken Chatbot")
|
|
|
141 |
</style>
|
142 |
""")
|
143 |
|
144 |
+
# Launch the Gradio interface
|
145 |
demo.launch()
|