Update app.py
Browse files
app.py
CHANGED
|
@@ -1,162 +1,132 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import random
|
| 3 |
-
import torch
|
| 4 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 5 |
-
from fastapi import FastAPI, Request
|
| 6 |
-
from fastapi.responses import JSONResponse
|
| 7 |
-
import uvicorn
|
| 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 |
-
return random.choice(greetings)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# -------------------------------
|
| 38 |
-
# IMAGE CAPTIONING (BLIP MODEL)
|
| 39 |
-
# -------------------------------
|
| 40 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 41 |
-
try:
|
| 42 |
-
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 43 |
-
model = BlipForConditionalGeneration.from_pretrained(
|
| 44 |
-
"Salesforce/blip-image-captioning-base"
|
| 45 |
-
).to(device)
|
| 46 |
-
print("BLIP models loaded successfully.")
|
| 47 |
-
except Exception as e:
|
| 48 |
-
print("⚠️ Failed to load BLIP model:", e)
|
| 49 |
-
|
| 50 |
-
def generate_caption(image):
|
| 51 |
-
try:
|
| 52 |
-
inputs = processor(image, return_tensors="pt").to(device)
|
| 53 |
-
out = model.generate(**inputs)
|
| 54 |
-
return processor.decode(out[0], skip_special_tokens=True)
|
| 55 |
-
except Exception as e:
|
| 56 |
-
return f"Error generating caption: {e}"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# -------------------------------
|
| 60 |
-
# CHATBOT CORE LOGIC
|
| 61 |
-
# -------------------------------
|
| 62 |
-
def chat_response(message, image=None, file=None, audio=None):
|
| 63 |
-
if image is not None:
|
| 64 |
-
return generate_caption(image)
|
| 65 |
-
if message:
|
| 66 |
-
responses = [
|
| 67 |
-
"That's an interesting thought. Tell me more.",
|
| 68 |
-
"Let's dive deeper — what are you aiming to achieve?",
|
| 69 |
-
"Noted. Let’s explore your next move.",
|
| 70 |
-
"Hmm... that could lead somewhere powerful.",
|
| 71 |
-
"I see where you're going. Let's refine it.",
|
| 72 |
-
]
|
| 73 |
-
return random.choice(responses)
|
| 74 |
-
return "👋 Hello there!"
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# -------------------------------
|
| 78 |
-
# BUILD GRADIO UI
|
| 79 |
-
# -------------------------------
|
| 80 |
def create_ui():
|
| 81 |
with gr.Blocks(
|
| 82 |
-
theme=gr.themes.Soft(
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
css="""
|
| 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 |
) as demo:
|
| 110 |
-
gr.Markdown(f"### {get_dynamic_greeting()}")
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
chatbot = gr.Chatbot(
|
|
|
|
| 113 |
elem_id="chatbox",
|
| 114 |
-
label="Chat with AID",
|
| 115 |
type="messages"
|
| 116 |
)
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
)
|
| 126 |
-
mic = gr.Audio(label=None, show_label=False)
|
| 127 |
-
attach = gr.File(label=None)
|
| 128 |
-
image = gr.Image(label=None, type="pil")
|
| 129 |
-
|
| 130 |
-
with gr.Column(scale=2, min_width=80):
|
| 131 |
-
send_btn = gr.Button("Send 🚀", variant="primary")
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
history.append({"role": "user", "content": message})
|
| 137 |
-
history.append({"role": "assistant", "content": reply})
|
| 138 |
-
return "", history
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
[txt, chatbot, image, attach, mic],
|
| 143 |
-
[txt, chatbot],
|
| 144 |
-
)
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
)
|
| 151 |
|
| 152 |
return demo
|
| 153 |
|
| 154 |
-
|
| 155 |
-
# -------------------------------
|
| 156 |
-
# APP LAUNCH
|
| 157 |
-
# -------------------------------
|
| 158 |
-
demo = create_ui()
|
| 159 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
| 160 |
-
|
| 161 |
if __name__ == "__main__":
|
| 162 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load BLIP model
|
| 6 |
+
blip_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 7 |
+
print("✅ BLIP model loaded successfully.")
|
| 8 |
+
|
| 9 |
+
# Dynamic greetings
|
| 10 |
+
greetings = [
|
| 11 |
+
"⚡ A new chat, a new chance to shape reality.",
|
| 12 |
+
"🌍 Let’s make something remarkable today.",
|
| 13 |
+
"💫 Welcome back — let’s imagine boldly.",
|
| 14 |
+
"🔥 The future begins with your next message.",
|
| 15 |
+
"✨ Every word you type shapes what comes next."
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
# --- Core chat logic ---
|
| 19 |
+
def respond(message, history):
|
| 20 |
+
history = history or []
|
| 21 |
+
bot_reply = f"AID 🤖: I received — {message}"
|
| 22 |
+
history.append(("👤 You: " + message, bot_reply))
|
| 23 |
+
return history, gr.update(value="")
|
| 24 |
+
|
| 25 |
+
# --- Reset chat ---
|
| 26 |
+
def new_chat():
|
| 27 |
+
return [], gr.update(value=random.choice(greetings))
|
| 28 |
+
|
| 29 |
+
# --- UI Builder ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def create_ui():
|
| 31 |
with gr.Blocks(
|
| 32 |
+
theme=gr.themes.Soft(
|
| 33 |
+
primary_hue="blue",
|
| 34 |
+
secondary_hue="violet",
|
| 35 |
+
neutral_hue="gray"
|
| 36 |
+
),
|
| 37 |
css="""
|
| 38 |
+
body {
|
| 39 |
+
background: var(--background-fill-primary);
|
| 40 |
+
font-family: 'Inter', sans-serif;
|
| 41 |
+
}
|
| 42 |
+
#chatbox {
|
| 43 |
+
height: 470px !important;
|
| 44 |
+
overflow-y: auto;
|
| 45 |
+
background: var(--background-fill-secondary);
|
| 46 |
+
border-radius: 12px;
|
| 47 |
+
padding: 10px;
|
| 48 |
+
}
|
| 49 |
+
.message-row {
|
| 50 |
+
display: flex;
|
| 51 |
+
margin: 8px 0;
|
| 52 |
+
animation: fadeIn 0.3s ease-in-out;
|
| 53 |
+
}
|
| 54 |
+
.message-user {
|
| 55 |
+
justify-content: flex-end;
|
| 56 |
+
}
|
| 57 |
+
.message-bot {
|
| 58 |
+
justify-content: flex-start;
|
| 59 |
+
}
|
| 60 |
+
.bubble {
|
| 61 |
+
max-width: 75%;
|
| 62 |
+
padding: 10px 14px;
|
| 63 |
+
border-radius: 18px;
|
| 64 |
+
line-height: 1.4;
|
| 65 |
+
font-size: 15px;
|
| 66 |
+
}
|
| 67 |
+
.user-bubble {
|
| 68 |
+
background-color: #E5E7EB;
|
| 69 |
+
color: #111827;
|
| 70 |
+
border-bottom-right-radius: 4px;
|
| 71 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 72 |
+
}
|
| 73 |
+
.bot-bubble {
|
| 74 |
+
background-color: #2563EB;
|
| 75 |
+
color: white;
|
| 76 |
+
border-bottom-left-radius: 4px;
|
| 77 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 78 |
+
}
|
| 79 |
+
#newchat {
|
| 80 |
+
position: fixed; bottom: 20px; right: 20px;
|
| 81 |
+
z-index: 100; background: #2563EB;
|
| 82 |
+
color: white; font-weight: bold;
|
| 83 |
+
border-radius: 50%; width: 55px; height: 55px;
|
| 84 |
+
display: flex; align-items: center; justify-content: center;
|
| 85 |
+
font-size: 28px;
|
| 86 |
+
box-shadow: 0 4px 10px rgba(0,0,0,0.3);
|
| 87 |
+
cursor: pointer; transition: all 0.2s ease-in-out;
|
| 88 |
+
}
|
| 89 |
+
#newchat:hover {background: #1D4ED8;}
|
| 90 |
+
@keyframes fadeIn {
|
| 91 |
+
from {opacity: 0; transform: translateY(8px);}
|
| 92 |
+
to {opacity: 1; transform: translateY(0);}
|
| 93 |
+
}
|
| 94 |
+
""",
|
| 95 |
) as demo:
|
|
|
|
| 96 |
|
| 97 |
+
# --- Header ---
|
| 98 |
+
title = gr.Markdown(f"### {random.choice(greetings)}", elem_id="welcome")
|
| 99 |
+
|
| 100 |
+
# --- Chat Display ---
|
| 101 |
chatbot = gr.Chatbot(
|
| 102 |
+
label="💬 Chat with AID",
|
| 103 |
elem_id="chatbox",
|
|
|
|
| 104 |
type="messages"
|
| 105 |
)
|
| 106 |
|
| 107 |
with gr.Row():
|
| 108 |
+
msg = gr.Textbox(
|
| 109 |
+
placeholder="Type your message...",
|
| 110 |
+
show_label=False,
|
| 111 |
+
scale=8
|
| 112 |
+
)
|
| 113 |
+
send_btn = gr.Button("Send", scale=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
# --- Optional Audio ---
|
| 116 |
+
with gr.Accordion("🎙 Audio Input (Optional)", open=False):
|
| 117 |
+
mic = gr.Audio(label="Upload or Record Audio", type="filepath")
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
# --- Floating New Chat Button ---
|
| 120 |
+
newchat_btn = gr.Button("+", elem_id="newchat")
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
# Bind actions
|
| 123 |
+
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
| 124 |
+
send_btn.click(respond, [msg, chatbot], [chatbot, msg])
|
| 125 |
+
newchat_btn.click(new_chat, outputs=[chatbot, title])
|
|
|
|
| 126 |
|
| 127 |
return demo
|
| 128 |
|
| 129 |
+
# --- Launch app ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
if __name__ == "__main__":
|
| 131 |
+
demo = create_ui()
|
| 132 |
+
demo.launch()
|