File size: 4,009 Bytes
ddb0e33
b0397d2
e345a10
941c848
 
 
7fb780e
 
b0397d2
e3682f7
 
 
 
1c76710
e3682f7
 
 
 
 
 
 
 
 
 
 
 
7fb780e
 
941c848
7fb780e
941c848
7fb780e
941c848
 
 
 
 
 
 
7fb780e
941c848
7fb780e
ddb0e33
7fb780e
 
91e0dca
7fb780e
 
 
e345a10
941c848
7fb780e
6f9707c
7fb780e
 
 
941c848
 
7fb780e
941c848
 
 
 
 
 
 
7fb780e
c830a02
7fb780e
 
16f0ae2
c830a02
e1037bf
7fb780e
941c848
7fb780e
941c848
7fb780e
941c848
c830a02
 
ddb0e33
7fb780e
 
 
 
 
 
 
 
 
 
 
b0397d2
7fb780e
39690f6
 
d60af30
 
 
 
 
 
dd02d97
de24765
504278a
d60af30
 
dd02d97
 
 
d60af30
de24765
 
 
 
d60af30
de24765
 
39690f6
de24765
 
7fb780e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
import gradio as gr
from langchain_google_genai.chat_models import ChatGoogleGenerativeAI  # Import for Gemini
from PIL import Image
import torch
from torchvision import models, transforms
import json
import requests

# Load credentials (stringified JSON) from environment variable
credentials_string = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
if not credentials_string:
    raise ValueError("GOOGLE_APPLICATION_CREDENTIALS is not set in the environment!")

# Parse the stringified JSON back to a Python dictionary
credentials = json.loads(credentials_string)

# Save the credentials to a temporary JSON file (required by Google SDKs)
with open("service_account.json", "w") as f:
    json.dump(credentials, f)

# Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the temporary file
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service_account.json"

# Initialize Gemini model
llm = ChatGoogleGenerativeAI(model='gemini-1.5-pro')

# Load a pre-trained ResNet50 model for image classification
model = models.resnet50(pretrained=True)
model.eval()

# Transformation pipeline for image preprocessing
transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Load ImageNet labels
LABELS_URL = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"
labels = json.loads(requests.get(LABELS_URL).text)

# Global chat history variable
chat_history = []

def chat_with_gemini(message):
    global chat_history
    # Get a response from the language model
    bot_response = llm.predict(message)  # This will interact with the Gemini model
    chat_history.append((message, bot_response))
    return chat_history

def analyze_image(image_path):
    global chat_history
    # Open, preprocess, and classify the image
    image = Image.open(image_path).convert("RGB")
    image_tensor = transform(image).unsqueeze(0)

    with torch.no_grad():
        outputs = model(image_tensor)
        _, predicted_idx = outputs.max(1)
    
    label = labels[predicted_idx.item()]
    bot_response = f"The image seems to be: {label}."
    chat_history.append(("Uploaded an image for analysis", bot_response))
    return chat_history

# Build the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Ken Chatbot")
    gr.Markdown("Ask me anything or upload an image for analysis!")

    # Chatbot display without "User" or "Bot" labels
    chatbot = gr.Chatbot(elem_id="chatbot")

    # User input components
    msg = gr.Textbox(label="Type your message here...", placeholder="Enter your message...", show_label=False)
    send_btn = gr.Button("Send")
    img_upload = gr.Image(type="filepath", label="Upload an image for analysis")

    # Define interactions
    def handle_text_message(message):
        return chat_with_gemini(message)

    def handle_image_upload(image_path):
        return analyze_image(image_path)

    # Set up Gradio components with Enter key for sending
    msg.submit(handle_text_message, msg, chatbot)
    send_btn.click(handle_text_message, msg, chatbot)
    send_btn.click(lambda: "", None, msg)  # Clear input field
    img_upload.change(handle_image_upload, img_upload, chatbot)

    # Custom CSS for styling without usernames
    gr.HTML("""
    <style>
    #chatbot .message-container {
        display: flex;
        flex-direction: column;
        margin-bottom: 10px;
        max-width: 70%;
    }
    #chatbot .message {
        border-radius: 15px;
        padding: 10px;
        margin: 5px 0;
        word-wrap: break-word;
    }
    #chatbot .message.user {
        background-color: #DCF8C6;
        margin-left: auto;
        text-align: right;
    }
    #chatbot .message.bot {
        background-color: #E1E1E1;
        margin-right: auto;
        text-align: left;
    }
    </style>
    """)

# Launch for Hugging Face Spaces
demo.launch()