Spaces:
Runtime error
Runtime error
David Ko
commited on
Commit
ยท
e821ec3
1
Parent(s):
d10dea2
Fix: LLM query token length limit issue
Browse files
api.py
CHANGED
|
@@ -98,9 +98,24 @@ def process_llm_query(vision_results, user_query):
|
|
| 98 |
if llm_model is None or llm_tokenizer is None:
|
| 99 |
return {"error": "LLM model not available"}
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# Create a prompt combining vision results and user query
|
| 102 |
prompt = f"""You are an AI assistant analyzing image detection results.
|
| 103 |
-
Here are the objects detected in the image: {json.dumps(
|
| 104 |
|
| 105 |
User question: {user_query}
|
| 106 |
|
|
@@ -111,6 +126,17 @@ def process_llm_query(vision_results, user_query):
|
|
| 111 |
try:
|
| 112 |
start_time = time.time()
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
inputs = llm_tokenizer(prompt, return_tensors="pt").to(device)
|
| 115 |
with torch.no_grad():
|
| 116 |
output = llm_model.generate(
|
|
|
|
| 98 |
if llm_model is None or llm_tokenizer is None:
|
| 99 |
return {"error": "LLM model not available"}
|
| 100 |
|
| 101 |
+
# ๊ฒฐ๊ณผ ๋ฐ์ดํฐ ์์ฝ (ํ ํฐ ๊ธธ์ด ์ ํ์ ์ํด)
|
| 102 |
+
summarized_results = []
|
| 103 |
+
|
| 104 |
+
# ๊ฐ์ฒด ํ์ง ๊ฒฐ๊ณผ ์์ฝ
|
| 105 |
+
if isinstance(vision_results, list):
|
| 106 |
+
# ์ต๋ 10๊ฐ ๊ฐ์ฒด๋ง ํฌํจ
|
| 107 |
+
for i, obj in enumerate(vision_results[:10]):
|
| 108 |
+
if isinstance(obj, dict):
|
| 109 |
+
# ํ์ํ ์ ๋ณด๋ง ์ถ์ถ
|
| 110 |
+
summary = {
|
| 111 |
+
"label": obj.get("label", "unknown"),
|
| 112 |
+
"confidence": obj.get("confidence", 0),
|
| 113 |
+
}
|
| 114 |
+
summarized_results.append(summary)
|
| 115 |
+
|
| 116 |
# Create a prompt combining vision results and user query
|
| 117 |
prompt = f"""You are an AI assistant analyzing image detection results.
|
| 118 |
+
Here are the objects detected in the image: {json.dumps(summarized_results, indent=2)}
|
| 119 |
|
| 120 |
User question: {user_query}
|
| 121 |
|
|
|
|
| 126 |
try:
|
| 127 |
start_time = time.time()
|
| 128 |
|
| 129 |
+
# ํ ํฐ ๊ธธ์ด ํ์ธ ๋ฐ ์ ํ
|
| 130 |
+
tokens = llm_tokenizer.encode(prompt)
|
| 131 |
+
if len(tokens) > 1500: # ์์ ๋ง์ง ์ค์
|
| 132 |
+
prompt = f"""You are an AI assistant analyzing image detection results.
|
| 133 |
+
The image contains {len(summarized_results)} detected objects.
|
| 134 |
+
|
| 135 |
+
User question: {user_query}
|
| 136 |
+
|
| 137 |
+
Please provide a general analysis based on the user's question.
|
| 138 |
+
"""
|
| 139 |
+
|
| 140 |
inputs = llm_tokenizer(prompt, return_tensors="pt").to(device)
|
| 141 |
with torch.no_grad():
|
| 142 |
output = llm_model.generate(
|