Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,29 +1,29 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
from PIL import Image
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
os.environ["GEMINI_API_KEY"] = "
|
| 9 |
|
| 10 |
-
# Import VietMEAgent
|
| 11 |
try:
|
| 12 |
from core.viet_meagent import VietMEAgent
|
| 13 |
agent = VietMEAgent()
|
| 14 |
DEMO_MODE = False
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
-
print(f"
|
| 17 |
DEMO_MODE = True
|
| 18 |
|
| 19 |
def process_image(image, question):
|
| 20 |
"""Process image and question through VietMEAgent"""
|
| 21 |
if DEMO_MODE:
|
| 22 |
return {
|
| 23 |
-
"answer": "
|
| 24 |
-
"explanation": "VietMEAgent
|
| 25 |
-
"cultural_objects": ["
|
| 26 |
-
"confidence": 0.
|
| 27 |
}
|
| 28 |
|
| 29 |
try:
|
|
@@ -55,7 +55,7 @@ def process_image(image, question):
|
|
| 55 |
except Exception as e:
|
| 56 |
return {
|
| 57 |
"answer": f"Lỗi: {str(e)}",
|
| 58 |
-
"explanation": "Vui lòng thử lại hoặc kiểm tra
|
| 59 |
"cultural_objects": [],
|
| 60 |
"confidence": 0.0
|
| 61 |
}
|
|
@@ -75,32 +75,71 @@ def main_interface(image, question):
|
|
| 75 |
)
|
| 76 |
|
| 77 |
# Create Gradio interface
|
| 78 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
gr.Markdown("""
|
| 80 |
-
#
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
""")
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
-
with gr.Column():
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
question_input = gr.Textbox(
|
| 93 |
-
label="Câu hỏi (Tiếng Việt)",
|
| 94 |
-
placeholder="Đây là gì? / Món ăn này có ý nghĩa
|
| 95 |
-
value="Đây là gì?"
|
|
|
|
| 96 |
)
|
| 97 |
-
submit_btn = gr.Button("🔍 Phân tích", variant="primary")
|
| 98 |
|
| 99 |
-
with gr.Column():
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
submit_btn.click(
|
| 106 |
fn=main_interface,
|
|
@@ -108,18 +147,41 @@ with gr.Blocks(title="🇻🇳 VietMEAgent - Vietnamese Cultural VQA") as demo:
|
|
| 108 |
outputs=[answer_output, explanation_output, cultural_objects_output, confidence_output]
|
| 109 |
)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
gr.Markdown("""
|
| 112 |
-
## 📚
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
```
|
| 122 |
""")
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
from PIL import Image
|
| 4 |
import tempfile
|
| 5 |
|
| 6 |
+
# Production API key
|
| 7 |
+
os.environ["GEMINI_API_KEY"] = "AIzaSyCgatP7izHkaBn6im8AfXq0Ufmb0Fr-7dc"
|
| 8 |
|
| 9 |
+
# Import VietMEAgent
|
| 10 |
try:
|
| 11 |
from core.viet_meagent import VietMEAgent
|
| 12 |
agent = VietMEAgent()
|
| 13 |
DEMO_MODE = False
|
| 14 |
+
print("✅ VietMEAgent initialized successfully!")
|
| 15 |
except Exception as e:
|
| 16 |
+
print(f"❌ VietMEAgent initialization failed: {e}")
|
| 17 |
DEMO_MODE = True
|
| 18 |
|
| 19 |
def process_image(image, question):
|
| 20 |
"""Process image and question through VietMEAgent"""
|
| 21 |
if DEMO_MODE:
|
| 22 |
return {
|
| 23 |
+
"answer": "System initialization failed. Please check API key configuration.",
|
| 24 |
+
"explanation": "VietMEAgent could not be initialized. Please ensure GEMINI_API_KEY is set correctly.",
|
| 25 |
+
"cultural_objects": ["error"],
|
| 26 |
+
"confidence": 0.0
|
| 27 |
}
|
| 28 |
|
| 29 |
try:
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
return {
|
| 57 |
"answer": f"Lỗi: {str(e)}",
|
| 58 |
+
"explanation": "Vui lòng thử lại hoặc kiểm tra kết nối mạng",
|
| 59 |
"cultural_objects": [],
|
| 60 |
"confidence": 0.0
|
| 61 |
}
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
# Create Gradio interface
|
| 78 |
+
with gr.Blocks(
|
| 79 |
+
title="🏛️ VietMEAgent - Vietnamese Cultural VQA",
|
| 80 |
+
theme=gr.themes.Soft(),
|
| 81 |
+
css="""
|
| 82 |
+
.gradio-container {
|
| 83 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 84 |
+
}
|
| 85 |
+
.gr-button-primary {
|
| 86 |
+
background: linear-gradient(45deg, #ff6b6b, #ffa500);
|
| 87 |
+
border: none;
|
| 88 |
+
}
|
| 89 |
+
"""
|
| 90 |
+
) as demo:
|
| 91 |
gr.Markdown("""
|
| 92 |
+
# 🏛️ VietMEAgent: Vietnamese Multimodal Explanation Agent
|
| 93 |
|
| 94 |
+
**🇻🇳 Hệ thống AI trả lời câu hỏi về hình ảnh văn hóa Việt Nam**
|
| 95 |
|
| 96 |
+
Được phát triển dựa trên nghiên cứu **FS-MEVQA (ACM MM 2024)** với khả năng:
|
| 97 |
+
- 🔍 Phân tích hình ảnh văn hóa Việt Nam
|
| 98 |
+
- 💬 Trả lời câu hỏi bằng tiếng Việt
|
| 99 |
+
- 🎯 Giải thích có bối cảnh văn hóa
|
| 100 |
+
- 🏛️ Nhận diện 50+ đối tượng văn hóa truyền thống
|
| 101 |
|
| 102 |
+
---
|
| 103 |
""")
|
| 104 |
|
| 105 |
with gr.Row():
|
| 106 |
+
with gr.Column(scale=1):
|
| 107 |
+
gr.Markdown("### 📤 Input")
|
| 108 |
+
image_input = gr.Image(
|
| 109 |
+
type="pil",
|
| 110 |
+
label="🖼️ Hình ảnh văn hóa Việt Nam",
|
| 111 |
+
height=300
|
| 112 |
+
)
|
| 113 |
question_input = gr.Textbox(
|
| 114 |
+
label="❓ Câu hỏi (Tiếng Việt)",
|
| 115 |
+
placeholder="Đây là gì? / Món ăn này có ý nghĩa gì trong văn hóa Việt Nam?",
|
| 116 |
+
value="Đây là gì?",
|
| 117 |
+
lines=2
|
| 118 |
)
|
| 119 |
+
submit_btn = gr.Button("🔍 Phân tích Cultural VQA", variant="primary", size="lg")
|
| 120 |
|
| 121 |
+
with gr.Column(scale=1):
|
| 122 |
+
gr.Markdown("### 📊 Results")
|
| 123 |
+
answer_output = gr.Textbox(
|
| 124 |
+
label="🎯 Câu trả lời",
|
| 125 |
+
lines=3,
|
| 126 |
+
interactive=False
|
| 127 |
+
)
|
| 128 |
+
explanation_output = gr.Textbox(
|
| 129 |
+
label="📝 Giải thích văn hóa",
|
| 130 |
+
lines=5,
|
| 131 |
+
interactive=False
|
| 132 |
+
)
|
| 133 |
+
cultural_objects_output = gr.Textbox(
|
| 134 |
+
label="🏛️ Đối tượng văn hóa phát hiện",
|
| 135 |
+
lines=2,
|
| 136 |
+
interactive=False
|
| 137 |
+
)
|
| 138 |
+
confidence_output = gr.Textbox(
|
| 139 |
+
label="📊 Độ tin cậy",
|
| 140 |
+
lines=1,
|
| 141 |
+
interactive=False
|
| 142 |
+
)
|
| 143 |
|
| 144 |
submit_btn.click(
|
| 145 |
fn=main_interface,
|
|
|
|
| 147 |
outputs=[answer_output, explanation_output, cultural_objects_output, confidence_output]
|
| 148 |
)
|
| 149 |
|
| 150 |
+
with gr.Row():
|
| 151 |
+
gr.Examples(
|
| 152 |
+
examples=[
|
| 153 |
+
["data_sample/images/am_thuc/000040.jpg", "Món ăn này có nguồn gốc từ đâu?"],
|
| 154 |
+
["data_sample/images/kien_truc/000042.jpg", "Kiến trúc này thể hiện phong cách gì?"],
|
| 155 |
+
["data_sample/images/phong_canh/000040.jpg", "Đây là danh lam thắng cảnh nào của Việt Nam?"]
|
| 156 |
+
],
|
| 157 |
+
inputs=[image_input, question_input]
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
gr.Markdown("""
|
| 161 |
+
## 📚 Categories được hỗ trợ:
|
| 162 |
+
|
| 163 |
+
| 🍜 **Ẩm thực** | 🏛️ **Kiến trúc** | 👘 **Trang phục** | 🎭 **Lễ hội** |
|
| 164 |
+
|---|---|---|---|
|
| 165 |
+
| Phở, bánh mì, chè | Chùa, đình, nhà sàn | Áo dài, nón lá | Tết, Trung thu |
|
| 166 |
+
| **🎨 Thủ công** | **🎵 Nhạc cụ** | **🏞️ Phong cảnh** | **🎪 Dân gian** |
|
| 167 |
+
| Gốm sứ, thêu | Đàn bầu, trống | Vịnh Hạ Long | Múa lân, rối nước |
|
| 168 |
+
|
| 169 |
+
## 🔧 Technical Details:
|
| 170 |
+
- **Models**: CLIP, BLIP-VQA, Gemini, EasyOCR
|
| 171 |
+
- **Languages**: Vietnamese + English
|
| 172 |
+
- **Architecture**: Few-shot multimodal reasoning
|
| 173 |
+
- **Cultural KB**: 459+ Vietnamese cultural objects
|
| 174 |
+
|
| 175 |
+
---
|
| 176 |
|
| 177 |
+
📖 **Research**: Based on FS-MEVQA paper (ACM MM 2024)
|
| 178 |
+
👨💻 **Developer**: [@Dangindev](https://huggingface.co/Dangindev)
|
| 179 |
+
🔗 **GitHub**: Coming soon...
|
|
|
|
| 180 |
""")
|
| 181 |
|
| 182 |
if __name__ == "__main__":
|
| 183 |
+
demo.launch(
|
| 184 |
+
server_name="0.0.0.0",
|
| 185 |
+
server_port=7860,
|
| 186 |
+
share=True
|
| 187 |
+
)
|