Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,88 @@
|
|
1 |
import os
|
2 |
-
import random
|
3 |
import gradio as gr
|
4 |
from langchain_google_genai.chat_models import ChatGoogleGenerativeAI
|
|
|
|
|
|
|
5 |
|
6 |
-
# Set the
|
7 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./firm-catalyst-437006-s4-407500537db5.json"
|
8 |
-
|
9 |
-
# Initialize the language model with required parameters
|
10 |
llm = ChatGoogleGenerativeAI(model='gemini-1.5-pro')
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def chat_with_gemini(message, chat_history):
|
13 |
-
#
|
14 |
-
|
|
|
15 |
|
16 |
-
# Generate a response using the language model
|
17 |
-
bot_response = llm.predict(message) # Get the bot's response from the model
|
18 |
-
chat_history.append(("Bot", bot_response)) # Append bot's username and response
|
19 |
return chat_history, chat_history
|
20 |
|
21 |
-
def analyze_image(
|
22 |
-
#
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
# Provide a random response for image uploads
|
26 |
-
random_responses = [
|
27 |
-
"I can't analyze this image right now, but it looks interesting!",
|
28 |
-
"What a lovely image! But I can't quite figure it out.",
|
29 |
-
"This image seems unique, but I can't process it.",
|
30 |
-
"Interesting! Unfortunately, I can't analyze images yet."
|
31 |
-
]
|
32 |
-
bot_response = random.choice(random_responses) # Randomly choose a response
|
33 |
-
chat_history.append(("Bot", bot_response)) # Append bot's response
|
34 |
return chat_history, chat_history
|
35 |
|
36 |
-
# Create
|
37 |
with gr.Blocks() as iface:
|
38 |
gr.Markdown("# Ken Chatbot")
|
39 |
gr.Markdown("Ask me anything or upload an image for analysis!")
|
40 |
|
41 |
-
|
|
|
|
|
|
|
42 |
msg = gr.Textbox(label="Type your message here...", placeholder="Enter your message...")
|
43 |
-
send_btn = gr.Button("Send")
|
44 |
-
state = gr.State([]) # Store chat history
|
45 |
-
|
46 |
-
# Image upload component for image analysis
|
47 |
img_upload = gr.Image(type="filepath", label="Upload an image for analysis")
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
send_btn.click(lambda: "", None, msg) # Clear text box on submit
|
52 |
|
53 |
-
#
|
54 |
-
|
|
|
|
|
55 |
|
56 |
-
# Custom CSS for styling
|
57 |
gr.HTML("""
|
58 |
<style>
|
59 |
#chatbot .message-container {
|
@@ -62,11 +91,6 @@ with gr.Blocks() as iface:
|
|
62 |
margin-bottom: 10px;
|
63 |
max-width: 70%;
|
64 |
}
|
65 |
-
#chatbot .username {
|
66 |
-
font-weight: bold;
|
67 |
-
font-size: 12px;
|
68 |
-
margin-bottom: 2px;
|
69 |
-
}
|
70 |
#chatbot .message {
|
71 |
border-radius: 15px;
|
72 |
padding: 10px;
|
@@ -83,24 +107,8 @@ with gr.Blocks() as iface:
|
|
83 |
margin-right: auto;
|
84 |
text-align: left;
|
85 |
}
|
86 |
-
#chatbot .username.user {
|
87 |
-
color: #3C763D;
|
88 |
-
text-align: right;
|
89 |
-
}
|
90 |
-
#chatbot .username.bot {
|
91 |
-
color: #333;
|
92 |
-
text-align: left;
|
93 |
-
}
|
94 |
-
.username.user {
|
95 |
-
color: #3C763D;
|
96 |
-
text-align: right;
|
97 |
-
}
|
98 |
-
.username.bot {
|
99 |
-
color: #333;
|
100 |
-
text-align: left;
|
101 |
-
}
|
102 |
</style>
|
103 |
""")
|
104 |
|
105 |
-
# Launch the interface
|
106 |
iface.launch(debug=True)
|
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
from langchain_google_genai.chat_models import ChatGoogleGenerativeAI
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
from torchvision import models, transforms
|
7 |
|
8 |
+
# Set up the environment for Google Generative AI
|
9 |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./firm-catalyst-437006-s4-407500537db5.json"
|
|
|
|
|
10 |
llm = ChatGoogleGenerativeAI(model='gemini-1.5-pro')
|
11 |
|
12 |
+
# Load a pre-trained ResNet50 model for image analysis
|
13 |
+
model = models.resnet50(pretrained=True)
|
14 |
+
model.eval() # Set the model to evaluation mode
|
15 |
+
|
16 |
+
# Define the transformation for the image
|
17 |
+
transform = transforms.Compose([
|
18 |
+
transforms.Resize(256),
|
19 |
+
transforms.CenterCrop(224),
|
20 |
+
transforms.ToTensor(),
|
21 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
22 |
+
])
|
23 |
+
|
24 |
+
# Load the ImageNet labels
|
25 |
+
LABELS_URL = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"
|
26 |
+
labels = None
|
27 |
+
|
28 |
+
if not os.path.exists("imagenet_labels.json"):
|
29 |
+
import requests
|
30 |
+
response = requests.get(LABELS_URL)
|
31 |
+
with open("imagenet_labels.json", "wb") as f:
|
32 |
+
f.write(response.content)
|
33 |
+
|
34 |
+
import json
|
35 |
+
with open("imagenet_labels.json") as f:
|
36 |
+
labels = json.load(f)
|
37 |
+
|
38 |
def chat_with_gemini(message, chat_history):
|
39 |
+
# Generate a response from the language model
|
40 |
+
bot_response = llm.predict(message)
|
41 |
+
chat_history.append((message, bot_response))
|
42 |
|
|
|
|
|
|
|
43 |
return chat_history, chat_history
|
44 |
|
45 |
+
def analyze_image(image_path, chat_history):
|
46 |
+
# Load and preprocess the image
|
47 |
+
image = Image.open(image_path).convert("RGB")
|
48 |
+
image_tensor = transform(image).unsqueeze(0)
|
49 |
+
|
50 |
+
# Predict the image class
|
51 |
+
with torch.no_grad():
|
52 |
+
outputs = model(image_tensor)
|
53 |
+
_, predicted_idx = outputs.max(1)
|
54 |
+
|
55 |
+
# Retrieve the label
|
56 |
+
label = labels[predicted_idx.item()]
|
57 |
+
|
58 |
+
# Respond with the classification result
|
59 |
+
bot_response = f"The image seems to be: {label}."
|
60 |
+
chat_history.append(("Uploaded an image for analysis", bot_response))
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
return chat_history, chat_history
|
63 |
|
64 |
+
# Create Gradio interface
|
65 |
with gr.Blocks() as iface:
|
66 |
gr.Markdown("# Ken Chatbot")
|
67 |
gr.Markdown("Ask me anything or upload an image for analysis!")
|
68 |
|
69 |
+
# Chatbot component without usernames
|
70 |
+
chatbot = gr.Chatbot(elem_id="chatbot")
|
71 |
+
|
72 |
+
# User input components
|
73 |
msg = gr.Textbox(label="Type your message here...", placeholder="Enter your message...")
|
74 |
+
send_btn = gr.Button("Send")
|
|
|
|
|
|
|
75 |
img_upload = gr.Image(type="filepath", label="Upload an image for analysis")
|
76 |
|
77 |
+
# State for chat history
|
78 |
+
state = gr.State([])
|
|
|
79 |
|
80 |
+
# Define interactions
|
81 |
+
send_btn.click(chat_with_gemini, [msg, state], [chatbot, state]) # Handle text input
|
82 |
+
send_btn.click(lambda: "", None, msg) # Clear textbox
|
83 |
+
img_upload.change(analyze_image, [img_upload, state], [chatbot, state]) # Handle image uploads
|
84 |
|
85 |
+
# Custom CSS for styling chat bubbles without usernames
|
86 |
gr.HTML("""
|
87 |
<style>
|
88 |
#chatbot .message-container {
|
|
|
91 |
margin-bottom: 10px;
|
92 |
max-width: 70%;
|
93 |
}
|
|
|
|
|
|
|
|
|
|
|
94 |
#chatbot .message {
|
95 |
border-radius: 15px;
|
96 |
padding: 10px;
|
|
|
107 |
margin-right: auto;
|
108 |
text-align: left;
|
109 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
</style>
|
111 |
""")
|
112 |
|
113 |
+
# Launch the Gradio interface
|
114 |
iface.launch(debug=True)
|