Spaces:
Running
on
L4
Running
on
L4
Upload folder using huggingface_hub
Browse files
main.py
CHANGED
|
@@ -3,6 +3,7 @@ import hashlib
|
|
| 3 |
import time
|
| 4 |
from concurrent.futures import ThreadPoolExecutor
|
| 5 |
from functools import partial
|
|
|
|
| 6 |
|
| 7 |
from fasthtml.common import *
|
| 8 |
from shad4fast import *
|
|
@@ -77,6 +78,7 @@ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
|
| 77 |
GEMINI_SYSTEM_PROMPT = """If the user query is a question, try your best to answer it based on the provided images.
|
| 78 |
If the user query is not an obvious question, reply with 'No question detected.'. Your response should be HTML formatted.
|
| 79 |
This means that newlines will be replaced with <br> tags, bold text will be enclosed in <b> tags, and so on.
|
|
|
|
| 80 |
"""
|
| 81 |
gemini_model = genai.GenerativeModel(
|
| 82 |
"gemini-1.5-flash-8b", system_instruction=GEMINI_SYSTEM_PROMPT
|
|
@@ -291,19 +293,24 @@ async def full_image(docid: str, query_id: str, idx: int):
|
|
| 291 |
|
| 292 |
|
| 293 |
async def message_generator(query_id: str, query: str):
|
| 294 |
-
result = None
|
| 295 |
images = []
|
| 296 |
-
|
|
|
|
|
|
|
| 297 |
result = result_cache.get(query_id)
|
| 298 |
if result is None:
|
| 299 |
-
await asyncio.sleep(0.
|
| 300 |
continue
|
| 301 |
search_results = get_results_children(result)
|
| 302 |
for single_result in search_results:
|
| 303 |
img = single_result["fields"].get("full_image", None)
|
| 304 |
if img is not None:
|
| 305 |
images.append(img)
|
| 306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
# from b64 to PIL image
|
| 309 |
images = [Image.open(io.BytesIO(base64.b64decode(img))) for img in images]
|
|
|
|
| 3 |
import time
|
| 4 |
from concurrent.futures import ThreadPoolExecutor
|
| 5 |
from functools import partial
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
from fasthtml.common import *
|
| 9 |
from shad4fast import *
|
|
|
|
| 78 |
GEMINI_SYSTEM_PROMPT = """If the user query is a question, try your best to answer it based on the provided images.
|
| 79 |
If the user query is not an obvious question, reply with 'No question detected.'. Your response should be HTML formatted.
|
| 80 |
This means that newlines will be replaced with <br> tags, bold text will be enclosed in <b> tags, and so on.
|
| 81 |
+
But, you should NOT include backticks (`) or HTML tags in your response.
|
| 82 |
"""
|
| 83 |
gemini_model = genai.GenerativeModel(
|
| 84 |
"gemini-1.5-flash-8b", system_instruction=GEMINI_SYSTEM_PROMPT
|
|
|
|
| 293 |
|
| 294 |
|
| 295 |
async def message_generator(query_id: str, query: str):
|
|
|
|
| 296 |
images = []
|
| 297 |
+
result = None
|
| 298 |
+
all_images_ready = False
|
| 299 |
+
while not all_images_ready:
|
| 300 |
result = result_cache.get(query_id)
|
| 301 |
if result is None:
|
| 302 |
+
await asyncio.sleep(0.1)
|
| 303 |
continue
|
| 304 |
search_results = get_results_children(result)
|
| 305 |
for single_result in search_results:
|
| 306 |
img = single_result["fields"].get("full_image", None)
|
| 307 |
if img is not None:
|
| 308 |
images.append(img)
|
| 309 |
+
if len(images) == len(search_results):
|
| 310 |
+
all_images_ready = True
|
| 311 |
+
break
|
| 312 |
+
else:
|
| 313 |
+
await asyncio.sleep(0.1)
|
| 314 |
|
| 315 |
# from b64 to PIL image
|
| 316 |
images = [Image.open(io.BytesIO(base64.b64decode(img))) for img in images]
|