Spaces:
Runtime error
Runtime error
Upload 12 files
Browse files- .gitattributes +2 -0
- ContextualCompressionRetriever.ipynb +0 -0
- Retrieval.ipynb +588 -0
- SQL_s.docx +3 -0
- __pycache__/app.cpython-311.pyc +0 -0
- __pycache__/app.cpython-312.pyc +0 -0
- __pycache__/help.cpython-311.pyc +0 -0
- __pycache__/help.cpython-312.pyc +0 -0
- app.py +192 -0
- faiss_index/index.faiss +3 -0
- faiss_index/index.pkl +3 -0
- help.py +541 -0
- sql_eorror.png +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
faiss_index/index.faiss filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
SQL_s.docx filter=lfs diff=lfs merge=lfs -text
|
ContextualCompressionRetriever.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Retrieval.ipynb
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "aed563e2",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# Retrieval"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "code",
|
| 13 |
+
"execution_count": 20,
|
| 14 |
+
"id": "3c99cd9f",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [],
|
| 17 |
+
"source": [
|
| 18 |
+
"from langchain_community.vectorstores import FAISS\n",
|
| 19 |
+
"from langchain_ollama import OllamaEmbeddings\n",
|
| 20 |
+
"from langchain_ollama import ChatOllama\n",
|
| 21 |
+
"from langchain_core.prompts import PromptTemplate"
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"cell_type": "markdown",
|
| 26 |
+
"id": "002e9bea",
|
| 27 |
+
"metadata": {},
|
| 28 |
+
"source": [
|
| 29 |
+
"#### Load Vectore_store"
|
| 30 |
+
]
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"cell_type": "code",
|
| 34 |
+
"execution_count": null,
|
| 35 |
+
"id": "20eb543e",
|
| 36 |
+
"metadata": {},
|
| 37 |
+
"outputs": [],
|
| 38 |
+
"source": []
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"execution_count": 21,
|
| 43 |
+
"id": "93a1dad2",
|
| 44 |
+
"metadata": {},
|
| 45 |
+
"outputs": [],
|
| 46 |
+
"source": [
|
| 47 |
+
"embedding = OllamaEmbeddings(model=\"nomic-embed-text\")\n",
|
| 48 |
+
"\n",
|
| 49 |
+
"vector_store = FAISS.load_local(\n",
|
| 50 |
+
" \"faiss_index\",\n",
|
| 51 |
+
" embedding,\n",
|
| 52 |
+
" allow_dangerous_deserialization=True\n",
|
| 53 |
+
")"
|
| 54 |
+
]
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"cell_type": "code",
|
| 58 |
+
"execution_count": 22,
|
| 59 |
+
"id": "2aa58c7c",
|
| 60 |
+
"metadata": {},
|
| 61 |
+
"outputs": [],
|
| 62 |
+
"source": [
|
| 63 |
+
"retriever = vector_store.as_retriever(search_type=\"similarity\", search_kwargs={\"k\": 1})"
|
| 64 |
+
]
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"cell_type": "code",
|
| 68 |
+
"execution_count": 23,
|
| 69 |
+
"id": "4ddc68fb",
|
| 70 |
+
"metadata": {},
|
| 71 |
+
"outputs": [
|
| 72 |
+
{
|
| 73 |
+
"data": {
|
| 74 |
+
"text/plain": [
|
| 75 |
+
"VectorStoreRetriever(tags=['FAISS', 'OllamaEmbeddings'], vectorstore=<langchain_community.vectorstores.faiss.FAISS object at 0x000002832218BD10>, search_kwargs={'k': 1})"
|
| 76 |
+
]
|
| 77 |
+
},
|
| 78 |
+
"execution_count": 23,
|
| 79 |
+
"metadata": {},
|
| 80 |
+
"output_type": "execute_result"
|
| 81 |
+
}
|
| 82 |
+
],
|
| 83 |
+
"source": [
|
| 84 |
+
"retriever"
|
| 85 |
+
]
|
| 86 |
+
},
|
| 87 |
+
{
|
| 88 |
+
"cell_type": "code",
|
| 89 |
+
"execution_count": 24,
|
| 90 |
+
"id": "af9e08ab",
|
| 91 |
+
"metadata": {},
|
| 92 |
+
"outputs": [
|
| 93 |
+
{
|
| 94 |
+
"data": {
|
| 95 |
+
"text/plain": [
|
| 96 |
+
"[Document(id='d19b5983-6c58-4baa-9878-d1edcc1330f0', metadata={'topic': 'Distinct and Count Statement', 'question_no': 1}, page_content='1.Identify count of orders by counting ORDER_ID from ORDERS table. To count order_Id from table, you can write the following statement.\\n\\n\\n\\nselect \\n\\n count(order_id) \\n\\n from orders')]"
|
| 97 |
+
]
|
| 98 |
+
},
|
| 99 |
+
"execution_count": 24,
|
| 100 |
+
"metadata": {},
|
| 101 |
+
"output_type": "execute_result"
|
| 102 |
+
}
|
| 103 |
+
],
|
| 104 |
+
"source": [
|
| 105 |
+
"retriever.invoke('Identify count of orders by counting ORDER_ID from ORDERS table. To count order_Id from table, you can write the following statement')"
|
| 106 |
+
]
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"cell_type": "code",
|
| 110 |
+
"execution_count": null,
|
| 111 |
+
"id": "18a0d111",
|
| 112 |
+
"metadata": {},
|
| 113 |
+
"outputs": [],
|
| 114 |
+
"source": []
|
| 115 |
+
},
|
| 116 |
+
{
|
| 117 |
+
"cell_type": "markdown",
|
| 118 |
+
"id": "18e9e4bd",
|
| 119 |
+
"metadata": {},
|
| 120 |
+
"source": [
|
| 121 |
+
"# Step 3 - Augmentation"
|
| 122 |
+
]
|
| 123 |
+
},
|
| 124 |
+
{
|
| 125 |
+
"cell_type": "code",
|
| 126 |
+
"execution_count": 45,
|
| 127 |
+
"id": "bd69d69e",
|
| 128 |
+
"metadata": {},
|
| 129 |
+
"outputs": [],
|
| 130 |
+
"source": [
|
| 131 |
+
"llm = ChatOllama(\n",
|
| 132 |
+
" model=\"llama3.2\",\n",
|
| 133 |
+
" temperature=0\n",
|
| 134 |
+
")"
|
| 135 |
+
]
|
| 136 |
+
},
|
| 137 |
+
{
|
| 138 |
+
"cell_type": "code",
|
| 139 |
+
"execution_count": 46,
|
| 140 |
+
"id": "e4cef64b",
|
| 141 |
+
"metadata": {},
|
| 142 |
+
"outputs": [],
|
| 143 |
+
"source": [
|
| 144 |
+
"prompt = PromptTemplate(\n",
|
| 145 |
+
" template=\"\"\"\n",
|
| 146 |
+
"You are an SQL assistant.\n",
|
| 147 |
+
"\n",
|
| 148 |
+
"Use ONLY the provided context.\n",
|
| 149 |
+
"\n",
|
| 150 |
+
"Rules:\n",
|
| 151 |
+
"1. If the SQL query exists in the context, return ONLY that SQL query.\n",
|
| 152 |
+
"2. Do not explain.\n",
|
| 153 |
+
"3. Do not rewrite the question.\n",
|
| 154 |
+
"4. Do not generate a new query.\n",
|
| 155 |
+
"5. If the SQL query is missing, reply exactly:\n",
|
| 156 |
+
"I don't know.\n",
|
| 157 |
+
"\n",
|
| 158 |
+
"Context:\n",
|
| 159 |
+
"{context}\n",
|
| 160 |
+
"\n",
|
| 161 |
+
"Question:\n",
|
| 162 |
+
"{question}\n",
|
| 163 |
+
"\n",
|
| 164 |
+
"SQL:\n",
|
| 165 |
+
"\"\"\",\n",
|
| 166 |
+
" input_variables=[\"context\", \"question\"],\n",
|
| 167 |
+
")"
|
| 168 |
+
]
|
| 169 |
+
},
|
| 170 |
+
{
|
| 171 |
+
"cell_type": "code",
|
| 172 |
+
"execution_count": 47,
|
| 173 |
+
"id": "708dbc11",
|
| 174 |
+
"metadata": {},
|
| 175 |
+
"outputs": [],
|
| 176 |
+
"source": [
|
| 177 |
+
"question = \"You can see that counting customer_id is going to give you the same result. It means that even if you use count(*), you are going to get the same number of rows. Try using COUNT(*) this time to count the number of rows in the table \"\n"
|
| 178 |
+
]
|
| 179 |
+
},
|
| 180 |
+
{
|
| 181 |
+
"cell_type": "code",
|
| 182 |
+
"execution_count": 48,
|
| 183 |
+
"id": "dba263ce",
|
| 184 |
+
"metadata": {},
|
| 185 |
+
"outputs": [],
|
| 186 |
+
"source": [
|
| 187 |
+
"context_text = retriever.invoke(question)"
|
| 188 |
+
]
|
| 189 |
+
},
|
| 190 |
+
{
|
| 191 |
+
"cell_type": "code",
|
| 192 |
+
"execution_count": 49,
|
| 193 |
+
"id": "0f98f8d6",
|
| 194 |
+
"metadata": {},
|
| 195 |
+
"outputs": [
|
| 196 |
+
{
|
| 197 |
+
"data": {
|
| 198 |
+
"text/plain": [
|
| 199 |
+
"[Document(id='579858b1-f5c5-475c-8958-b16098c1a20b', metadata={'topic': 'Distinct and Count Statement', 'question_no': 3}, page_content='3.You can see that counting customer_id is going to give you the same result. It means that even if you use count(*), you are going to get the same number of rows. Try using COUNT(*) this time to count the number of rows in the table\\n\\n\\n\\n select \\n\\n count(*) \\n\\n from orders')]"
|
| 200 |
+
]
|
| 201 |
+
},
|
| 202 |
+
"execution_count": 49,
|
| 203 |
+
"metadata": {},
|
| 204 |
+
"output_type": "execute_result"
|
| 205 |
+
}
|
| 206 |
+
],
|
| 207 |
+
"source": [
|
| 208 |
+
"context_text"
|
| 209 |
+
]
|
| 210 |
+
},
|
| 211 |
+
{
|
| 212 |
+
"cell_type": "code",
|
| 213 |
+
"execution_count": 50,
|
| 214 |
+
"id": "d5878f7b",
|
| 215 |
+
"metadata": {},
|
| 216 |
+
"outputs": [],
|
| 217 |
+
"source": [
|
| 218 |
+
"final_prompt = prompt.invoke({\"context\": context_text, \"question\": question})"
|
| 219 |
+
]
|
| 220 |
+
},
|
| 221 |
+
{
|
| 222 |
+
"cell_type": "markdown",
|
| 223 |
+
"id": "a31f0f2e",
|
| 224 |
+
"metadata": {},
|
| 225 |
+
"source": [
|
| 226 |
+
"# Step 4 - Generation"
|
| 227 |
+
]
|
| 228 |
+
},
|
| 229 |
+
{
|
| 230 |
+
"cell_type": "code",
|
| 231 |
+
"execution_count": 51,
|
| 232 |
+
"id": "9166fe55",
|
| 233 |
+
"metadata": {},
|
| 234 |
+
"outputs": [
|
| 235 |
+
{
|
| 236 |
+
"name": "stdout",
|
| 237 |
+
"output_type": "stream",
|
| 238 |
+
"text": [
|
| 239 |
+
"select count(*) from orders\n"
|
| 240 |
+
]
|
| 241 |
+
}
|
| 242 |
+
],
|
| 243 |
+
"source": [
|
| 244 |
+
"answer = llm.invoke(final_prompt)\n",
|
| 245 |
+
"print(answer.content)"
|
| 246 |
+
]
|
| 247 |
+
},
|
| 248 |
+
{
|
| 249 |
+
"cell_type": "code",
|
| 250 |
+
"execution_count": null,
|
| 251 |
+
"id": "ffae664d",
|
| 252 |
+
"metadata": {},
|
| 253 |
+
"outputs": [],
|
| 254 |
+
"source": [
|
| 255 |
+
"prompt= PromptTemplate(\n",
|
| 256 |
+
" template=\"\"\"you have to extract the SQL query from the image and all the other dat from the . If the SQL query is not present in the context, return \"I don't know.\" Do not provide any explanations or additional information.\n",
|
| 257 |
+
" \n",
|
| 258 |
+
" \"\"\"\n",
|
| 259 |
+
" \n",
|
| 260 |
+
" )"
|
| 261 |
+
]
|
| 262 |
+
},
|
| 263 |
+
{
|
| 264 |
+
"cell_type": "code",
|
| 265 |
+
"execution_count": null,
|
| 266 |
+
"id": "13b8ce68",
|
| 267 |
+
"metadata": {},
|
| 268 |
+
"outputs": [],
|
| 269 |
+
"source": [
|
| 270 |
+
"response = chat(\n",
|
| 271 |
+
" model=\"qwen2.5vl:7b\",\n",
|
| 272 |
+
" messages=[\n",
|
| 273 |
+
" {\n",
|
| 274 |
+
" \"role\": \"user\",\n",
|
| 275 |
+
" \"content\": prompt,\n",
|
| 276 |
+
" \"images\": [image_path] # User uploaded image\n",
|
| 277 |
+
" }\n",
|
| 278 |
+
" ]\n",
|
| 279 |
+
" )"
|
| 280 |
+
]
|
| 281 |
+
},
|
| 282 |
+
{
|
| 283 |
+
"cell_type": "markdown",
|
| 284 |
+
"id": "deec7ff4",
|
| 285 |
+
"metadata": {},
|
| 286 |
+
"source": []
|
| 287 |
+
},
|
| 288 |
+
{
|
| 289 |
+
"cell_type": "code",
|
| 290 |
+
"execution_count": null,
|
| 291 |
+
"id": "6a8080fa",
|
| 292 |
+
"metadata": {},
|
| 293 |
+
"outputs": [],
|
| 294 |
+
"source": []
|
| 295 |
+
},
|
| 296 |
+
{
|
| 297 |
+
"cell_type": "markdown",
|
| 298 |
+
"id": "b020203d",
|
| 299 |
+
"metadata": {},
|
| 300 |
+
"source": [
|
| 301 |
+
"# Building a Chain"
|
| 302 |
+
]
|
| 303 |
+
},
|
| 304 |
+
{
|
| 305 |
+
"cell_type": "code",
|
| 306 |
+
"execution_count": 52,
|
| 307 |
+
"id": "f31cfbd8",
|
| 308 |
+
"metadata": {},
|
| 309 |
+
"outputs": [],
|
| 310 |
+
"source": [
|
| 311 |
+
"from langchain_core.runnables import RunnableParallel, RunnablePassthrough, RunnableLambda\n",
|
| 312 |
+
"from langchain_core.output_parsers import StrOutputParser"
|
| 313 |
+
]
|
| 314 |
+
},
|
| 315 |
+
{
|
| 316 |
+
"cell_type": "code",
|
| 317 |
+
"execution_count": 53,
|
| 318 |
+
"id": "3580a60a",
|
| 319 |
+
"metadata": {},
|
| 320 |
+
"outputs": [],
|
| 321 |
+
"source": [
|
| 322 |
+
"parallel_chain = RunnableParallel({\n",
|
| 323 |
+
" 'context': retriever ,\n",
|
| 324 |
+
" 'question': RunnablePassthrough()\n",
|
| 325 |
+
"})"
|
| 326 |
+
]
|
| 327 |
+
},
|
| 328 |
+
{
|
| 329 |
+
"cell_type": "code",
|
| 330 |
+
"execution_count": 55,
|
| 331 |
+
"id": "277f666d",
|
| 332 |
+
"metadata": {},
|
| 333 |
+
"outputs": [],
|
| 334 |
+
"source": [
|
| 335 |
+
"parser = StrOutputParser()"
|
| 336 |
+
]
|
| 337 |
+
},
|
| 338 |
+
{
|
| 339 |
+
"cell_type": "code",
|
| 340 |
+
"execution_count": 56,
|
| 341 |
+
"id": "aec127e2",
|
| 342 |
+
"metadata": {},
|
| 343 |
+
"outputs": [],
|
| 344 |
+
"source": [
|
| 345 |
+
"main_chain = parallel_chain | prompt | llm | parser"
|
| 346 |
+
]
|
| 347 |
+
},
|
| 348 |
+
{
|
| 349 |
+
"cell_type": "code",
|
| 350 |
+
"execution_count": 58,
|
| 351 |
+
"metadata": {},
|
| 352 |
+
"outputs": [
|
| 353 |
+
{
|
| 354 |
+
"data": {
|
| 355 |
+
"text/plain": [
|
| 356 |
+
"'select count(*) from orders'"
|
| 357 |
+
]
|
| 358 |
+
},
|
| 359 |
+
"execution_count": 58,
|
| 360 |
+
"metadata": {},
|
| 361 |
+
"output_type": "execute_result"
|
| 362 |
+
}
|
| 363 |
+
],
|
| 364 |
+
"source": [
|
| 365 |
+
"question = \"You can see that counting customer_id is going to give you the same result. It means that even if you use count(*), you are going to get the same number of rows. Try using COUNT(*) this time to count the number of rows in the table \"\n",
|
| 366 |
+
"main_chain.invoke(question)"
|
| 367 |
+
]
|
| 368 |
+
},
|
| 369 |
+
{
|
| 370 |
+
"cell_type": "code",
|
| 371 |
+
"execution_count": null,
|
| 372 |
+
"id": "f4372959",
|
| 373 |
+
"metadata": {},
|
| 374 |
+
"outputs": [],
|
| 375 |
+
"source": []
|
| 376 |
+
},
|
| 377 |
+
{
|
| 378 |
+
"cell_type": "code",
|
| 379 |
+
"execution_count": null,
|
| 380 |
+
"id": "1956a2ec",
|
| 381 |
+
"metadata": {},
|
| 382 |
+
"outputs": [],
|
| 383 |
+
"source": []
|
| 384 |
+
},
|
| 385 |
+
{
|
| 386 |
+
"cell_type": "code",
|
| 387 |
+
"execution_count": 59,
|
| 388 |
+
"id": "f9c5db7b",
|
| 389 |
+
"metadata": {},
|
| 390 |
+
"outputs": [],
|
| 391 |
+
"source": [
|
| 392 |
+
"from langchain_core.prompts import ChatPromptTemplate\n",
|
| 393 |
+
"from langchain_core.messages import HumanMessage\n",
|
| 394 |
+
"\n",
|
| 395 |
+
"# Define the extraction instructions\n",
|
| 396 |
+
"system_instructions = \"\"\"\n",
|
| 397 |
+
"Act as an expert OCR and data extraction assistant. I will provide you with an image of a user asking a coding question on a learning platform.\n",
|
| 398 |
+
"\n",
|
| 399 |
+
"Please format your response clearly using the following headings:\n",
|
| 400 |
+
"### User Statement\n",
|
| 401 |
+
"Extract the introductory text written by the user at the very top of the image.\n",
|
| 402 |
+
"\n",
|
| 403 |
+
"### Question Description\n",
|
| 404 |
+
"Extract the problem statement from the left-hand panel under the \"Description\" tab.\n",
|
| 405 |
+
"\n",
|
| 406 |
+
"### SQL Query\n",
|
| 407 |
+
"Extract the exact SQL code written inside the dark-themed code editor on the right. Preserve the line breaks and formatting.\n",
|
| 408 |
+
"\n",
|
| 409 |
+
"### Error Message\n",
|
| 410 |
+
"Extract the red error message displayed in the terminal/result area at the bottom right.\n",
|
| 411 |
+
"\"\"\"\n",
|
| 412 |
+
"\n",
|
| 413 |
+
"# Create the ChatPromptTemplate for multimodal input\n",
|
| 414 |
+
"multimodal_prompt = ChatPromptTemplate.from_messages([\n",
|
| 415 |
+
" (\"system\", system_instructions),\n",
|
| 416 |
+
" (\"user\", [\n",
|
| 417 |
+
" {\"type\": \"text\", \"text\": \"Please extract the requested data from this image.\"},\n",
|
| 418 |
+
" {\"type\": \"image_url\", \"image_url\": \"data:image/png;base64,{image_base64}\"}\n",
|
| 419 |
+
" ])\n",
|
| 420 |
+
"])\n",
|
| 421 |
+
"\n",
|
| 422 |
+
"# Example usage (assuming 'image_base64' is your base64 encoded string of the image):\n",
|
| 423 |
+
"# chain = multimodal_prompt | vision_llm\n",
|
| 424 |
+
"# result = chain.invoke({\"image_base64\": base64_string})"
|
| 425 |
+
]
|
| 426 |
+
},
|
| 427 |
+
{
|
| 428 |
+
"cell_type": "code",
|
| 429 |
+
"execution_count": 61,
|
| 430 |
+
"id": "9f054cc3",
|
| 431 |
+
"metadata": {},
|
| 432 |
+
"outputs": [
|
| 433 |
+
{
|
| 434 |
+
"ename": "ValidationError",
|
| 435 |
+
"evalue": "1 validation error for Message\ncontent\n Input should be a valid string [type=string_type, input_value=ChatPromptTemplate(input_... additional_kwargs={})]), input_type=ChatPromptTemplate]\n For further information visit https://errors.pydantic.dev/2.13/v/string_type",
|
| 436 |
+
"output_type": "error",
|
| 437 |
+
"traceback": [
|
| 438 |
+
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
| 439 |
+
"\u001b[31mValidationError\u001b[39m Traceback (most recent call last)",
|
| 440 |
+
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[61]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m ollama \u001b[38;5;28;01mimport\u001b[39;00m chat\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m response = chat(\n\u001b[32m 3\u001b[39m model=\u001b[33m\"qwen2.5vl:7b\"\u001b[39m,\n\u001b[32m 4\u001b[39m messages=[\n\u001b[32m 5\u001b[39m {\n",
|
| 441 |
+
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\p4pri\\anaconda3\\envs\\langchain311\\Lib\\site-packages\\ollama\\_client.py:393\u001b[39m, in \u001b[36mClient.chat\u001b[39m\u001b[34m(self, model, messages, tools, stream, think, logprobs, top_logprobs, format, options, keep_alive)\u001b[39m\n\u001b[32m 340\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mchat\u001b[39m(\n\u001b[32m 341\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m 342\u001b[39m model: \u001b[38;5;28mstr\u001b[39m = \u001b[33m'\u001b[39m\u001b[33m'\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 352\u001b[39m keep_alive: Optional[Union[\u001b[38;5;28mfloat\u001b[39m, \u001b[38;5;28mstr\u001b[39m]] = \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[32m 353\u001b[39m ) -> Union[ChatResponse, Iterator[ChatResponse]]:\n\u001b[32m 354\u001b[39m \u001b[38;5;250m \u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 355\u001b[39m \u001b[33;03m Create a chat response using the requested model.\u001b[39;00m\n\u001b[32m 356\u001b[39m \n\u001b[32m (...)\u001b[39m\u001b[32m 385\u001b[39m \u001b[33;03m Returns `ChatResponse` if `stream` is `False`, otherwise returns a `ChatResponse` generator.\u001b[39;00m\n\u001b[32m 386\u001b[39m \u001b[33;03m \"\"\"\u001b[39;00m\n\u001b[32m 387\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m._request(\n\u001b[32m 388\u001b[39m ChatResponse,\n\u001b[32m 389\u001b[39m \u001b[33m'\u001b[39m\u001b[33mPOST\u001b[39m\u001b[33m'\u001b[39m,\n\u001b[32m 390\u001b[39m \u001b[33m'\u001b[39m\u001b[33m/api/chat\u001b[39m\u001b[33m'\u001b[39m,\n\u001b[32m 391\u001b[39m json=ChatRequest(\n\u001b[32m 392\u001b[39m model=model,\n\u001b[32m--> \u001b[39m\u001b[32m393\u001b[39m messages=\u001b[30;43mlist\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43m_copy_messages\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43mmessages\u001b[39;49m\u001b[30;43m)\u001b[39;49m\u001b[30;43m)\u001b[39;49m,\n\u001b[32m 394\u001b[39m tools=\u001b[38;5;28mlist\u001b[39m(_copy_tools(tools)),\n\u001b[32m 395\u001b[39m stream=stream,\n\u001b[32m 396\u001b[39m think=think,\n\u001b[32m 397\u001b[39m logprobs=logprobs,\n\u001b[32m 398\u001b[39m top_logprobs=top_logprobs,\n\u001b[32m 399\u001b[39m \u001b[38;5;28mformat\u001b[39m=\u001b[38;5;28mformat\u001b[39m,\n\u001b[32m 400\u001b[39m options=options,\n\u001b[32m 401\u001b[39m keep_alive=keep_alive,\n\u001b[32m 402\u001b[39m ).model_dump(exclude_none=\u001b[38;5;28;01mTrue\u001b[39;00m),\n\u001b[32m 403\u001b[39m stream=stream,\n\u001b[32m 404\u001b[39m )\n",
|
| 442 |
+
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\p4pri\\anaconda3\\envs\\langchain311\\Lib\\site-packages\\ollama\\_client.py:1323\u001b[39m, in \u001b[36m_copy_messages\u001b[39m\u001b[34m(messages)\u001b[39m\n\u001b[32m 1321\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34m_copy_messages\u001b[39m(messages: Optional[Sequence[Union[Mapping[\u001b[38;5;28mstr\u001b[39m, Any], Message]]]) -> Iterator[Message]:\n\u001b[32m 1322\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m message \u001b[38;5;129;01min\u001b[39;00m messages \u001b[38;5;129;01mor\u001b[39;00m []:\n\u001b[32m-> \u001b[39m\u001b[32m1323\u001b[39m \u001b[38;5;28;01myield\u001b[39;00m \u001b[30;43mMessage\u001b[39;49m\u001b[30;43m.\u001b[39;49m\u001b[30;43mmodel_validate\u001b[39;49m\u001b[30;43m(\u001b[39;49m\n\u001b[32m 1324\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43m{\u001b[39;49m\u001b[30;43mk\u001b[39;49m\u001b[30;43m:\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43mlist\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43m_copy_images\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43mv\u001b[39;49m\u001b[30;43m)\u001b[39;49m\u001b[30;43m)\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43;01mif\u001b[39;49;00m\u001b[30;43m \u001b[39;49m\u001b[30;43mk\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43m==\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43m'\u001b[39;49m\u001b[30;43mimages\u001b[39;49m\u001b[30;43m'\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43;01melse\u001b[39;49;00m\u001b[30;43m \u001b[39;49m\u001b[30;43mv\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43;01mfor\u001b[39;49;00m\u001b[30;43m \u001b[39;49m\u001b[30;43mk\u001b[39;49m\u001b[30;43m,\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43mv\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43;01min\u001b[39;49;00m\u001b[30;43m \u001b[39;49m\u001b[30;43mdict\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43mmessage\u001b[39;49m\u001b[30;43m)\u001b[39;49m\u001b[30;43m.\u001b[39;49m\u001b[30;43mitems\u001b[39;49m\u001b[30;43m(\u001b[39;49m\u001b[30;43m)\u001b[39;49m\u001b[30;43m \u001b[39;49m\u001b[30;43;01mif\u001b[39;49;00m\u001b[30;43m \u001b[39;49m\u001b[30;43mv\u001b[39;49m\u001b[30;43m}\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 1325\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43m)\u001b[39;49m\n",
|
| 443 |
+
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\p4pri\\anaconda3\\envs\\langchain311\\Lib\\site-packages\\pydantic\\main.py:732\u001b[39m, in \u001b[36mBaseModel.model_validate\u001b[39m\u001b[34m(cls, obj, strict, extra, from_attributes, context, by_alias, by_name)\u001b[39m\n\u001b[32m 726\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m by_alias \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m by_name \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[32m 727\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m PydanticUserError(\n\u001b[32m 728\u001b[39m \u001b[33m'\u001b[39m\u001b[33mAt least one of `by_alias` or `by_name` must be set to True.\u001b[39m\u001b[33m'\u001b[39m,\n\u001b[32m 729\u001b[39m code=\u001b[33m'\u001b[39m\u001b[33mvalidate-by-alias-and-name-false\u001b[39m\u001b[33m'\u001b[39m,\n\u001b[32m 730\u001b[39m )\n\u001b[32m--> \u001b[39m\u001b[32m732\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[30;43mcls\u001b[39;49m\u001b[30;43m.\u001b[39;49m\u001b[30;43m__pydantic_validator__\u001b[39;49m\u001b[30;43m.\u001b[39;49m\u001b[30;43mvalidate_python\u001b[39;49m\u001b[30;43m(\u001b[39;49m\n\u001b[32m 733\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mobj\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 734\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mstrict\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mstrict\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 735\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mextra\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mextra\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 736\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mfrom_attributes\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mfrom_attributes\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 737\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mcontext\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mcontext\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 738\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mby_alias\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mby_alias\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 739\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mby_name\u001b[39;49m\u001b[30;43m=\u001b[39;49m\u001b[30;43mby_name\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 740\u001b[39m \u001b[30;43m\u001b[39;49m\u001b[30;43m)\u001b[39;49m\n",
|
| 444 |
+
"\u001b[31mValidationError\u001b[39m: 1 validation error for Message\ncontent\n Input should be a valid string [type=string_type, input_value=ChatPromptTemplate(input_... additional_kwargs={})]), input_type=ChatPromptTemplate]\n For further information visit https://errors.pydantic.dev/2.13/v/string_type"
|
| 445 |
+
]
|
| 446 |
+
}
|
| 447 |
+
],
|
| 448 |
+
"source": [
|
| 449 |
+
"from ollama import chat\n",
|
| 450 |
+
"response = chat(\n",
|
| 451 |
+
" model=\"qwen2.5vl:7b\",\n",
|
| 452 |
+
" messages=[\n",
|
| 453 |
+
" {\n",
|
| 454 |
+
" \"role\": \"user\",\n",
|
| 455 |
+
" \"content\": multimodal_prompt,\n",
|
| 456 |
+
" \"images\": ['sql_eorror.png'] # User uploaded image\n",
|
| 457 |
+
" }\n",
|
| 458 |
+
" ]\n",
|
| 459 |
+
" )"
|
| 460 |
+
]
|
| 461 |
+
},
|
| 462 |
+
{
|
| 463 |
+
"cell_type": "code",
|
| 464 |
+
"execution_count": null,
|
| 465 |
+
"id": "58284ff3",
|
| 466 |
+
"metadata": {},
|
| 467 |
+
"outputs": [],
|
| 468 |
+
"source": [
|
| 469 |
+
"response"
|
| 470 |
+
]
|
| 471 |
+
},
|
| 472 |
+
{
|
| 473 |
+
"cell_type": "code",
|
| 474 |
+
"execution_count": 62,
|
| 475 |
+
"id": "2c273a9c",
|
| 476 |
+
"metadata": {},
|
| 477 |
+
"outputs": [
|
| 478 |
+
{
|
| 479 |
+
"name": "stdout",
|
| 480 |
+
"output_type": "stream",
|
| 481 |
+
"text": [
|
| 482 |
+
"### User Statement\n",
|
| 483 |
+
"No user statement is provided in the image.\n",
|
| 484 |
+
"\n",
|
| 485 |
+
"### Question Description\n",
|
| 486 |
+
"The SQL script attempts to alter a table named 'CAL' by setting a column as NOT NULL and adding a PRIMARY KEY constraint, followed by creating an index on that column. However, it encounters errors due to constraints related to nullability of columns.\n",
|
| 487 |
+
"\n",
|
| 488 |
+
"### SQL Query\n",
|
| 489 |
+
"```sql\n",
|
| 490 |
+
"IF NOT EXISTS(\n",
|
| 491 |
+
" SELECT *\n",
|
| 492 |
+
" FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS\n",
|
| 493 |
+
" WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = 'CAL'\n",
|
| 494 |
+
")\n",
|
| 495 |
+
"BEGIN\n",
|
| 496 |
+
" DROP INDEX IF EXISTS CAL$01 ON dbo.CAL;\n",
|
| 497 |
+
" \n",
|
| 498 |
+
" ALTER TABLE BTS.dbo.CAL\n",
|
| 499 |
+
" ALTER COLUMN Intern \n",
|
| 500 |
+
" INT NOT NULL;\n",
|
| 501 |
+
"\n",
|
| 502 |
+
" ALTER TABLE BTS.dbo.CAL\n",
|
| 503 |
+
" ADD CONSTRAINT PK_CAL_Intern PRIMARY KEY (Intern);\n",
|
| 504 |
+
"\n",
|
| 505 |
+
" CREATE INDEX CAL$01\n",
|
| 506 |
+
" ON CAL (Intern);\n",
|
| 507 |
+
"END\n",
|
| 508 |
+
"```\n",
|
| 509 |
+
"\n",
|
| 510 |
+
"### Error Message\n",
|
| 511 |
+
"Msg 8111, Level 16, State 1, Line 111 \n",
|
| 512 |
+
"Cannot define PRIMARY KEY constraint on nullable column in table 'CAL'. \n",
|
| 513 |
+
"\n",
|
| 514 |
+
"Msg 1750, Level 16, State 0, Line 111 \n",
|
| 515 |
+
"Could not create constraint or index. See previous errors.\n"
|
| 516 |
+
]
|
| 517 |
+
}
|
| 518 |
+
],
|
| 519 |
+
"source": [
|
| 520 |
+
"from ollama import chat\n",
|
| 521 |
+
"\n",
|
| 522 |
+
"system_prompt = \"\"\"\n",
|
| 523 |
+
"Act as an expert OCR and data extraction assistant.\n",
|
| 524 |
+
"\n",
|
| 525 |
+
"Please format your response clearly using the following headings:\n",
|
| 526 |
+
"\n",
|
| 527 |
+
"### User Statement\n",
|
| 528 |
+
"Extract the introductory text written by the user at the very top of the image.\n",
|
| 529 |
+
"\n",
|
| 530 |
+
"### Question Description\n",
|
| 531 |
+
"Extract the problem statement from the left-hand panel under the Description tab.\n",
|
| 532 |
+
"\n",
|
| 533 |
+
"### SQL Query\n",
|
| 534 |
+
"Extract the exact SQL code written inside the dark-themed code editor on the right.\n",
|
| 535 |
+
"\n",
|
| 536 |
+
"### Error Message\n",
|
| 537 |
+
"Extract the red error message displayed at the bottom right.\n",
|
| 538 |
+
"\"\"\"\n",
|
| 539 |
+
"\n",
|
| 540 |
+
"response = chat(\n",
|
| 541 |
+
" model=\"qwen2.5vl:7b\",\n",
|
| 542 |
+
" messages=[\n",
|
| 543 |
+
" {\n",
|
| 544 |
+
" \"role\": \"system\",\n",
|
| 545 |
+
" \"content\": system_prompt\n",
|
| 546 |
+
" },\n",
|
| 547 |
+
" {\n",
|
| 548 |
+
" \"role\": \"user\",\n",
|
| 549 |
+
" \"content\": \"Extract all requested information from this image.\",\n",
|
| 550 |
+
" \"images\": [\"sql_eorror.png\"]\n",
|
| 551 |
+
" }\n",
|
| 552 |
+
" ]\n",
|
| 553 |
+
")\n",
|
| 554 |
+
"\n",
|
| 555 |
+
"print(response.message.content)"
|
| 556 |
+
]
|
| 557 |
+
},
|
| 558 |
+
{
|
| 559 |
+
"cell_type": "code",
|
| 560 |
+
"execution_count": null,
|
| 561 |
+
"id": "ee298127",
|
| 562 |
+
"metadata": {},
|
| 563 |
+
"outputs": [],
|
| 564 |
+
"source": []
|
| 565 |
+
}
|
| 566 |
+
],
|
| 567 |
+
"metadata": {
|
| 568 |
+
"kernelspec": {
|
| 569 |
+
"display_name": "langchain311",
|
| 570 |
+
"language": "python",
|
| 571 |
+
"name": "python3"
|
| 572 |
+
},
|
| 573 |
+
"language_info": {
|
| 574 |
+
"codemirror_mode": {
|
| 575 |
+
"name": "ipython",
|
| 576 |
+
"version": 3
|
| 577 |
+
},
|
| 578 |
+
"file_extension": ".py",
|
| 579 |
+
"mimetype": "text/x-python",
|
| 580 |
+
"name": "python",
|
| 581 |
+
"nbconvert_exporter": "python",
|
| 582 |
+
"pygments_lexer": "ipython3",
|
| 583 |
+
"version": "3.11.15"
|
| 584 |
+
}
|
| 585 |
+
},
|
| 586 |
+
"nbformat": 4,
|
| 587 |
+
"nbformat_minor": 5
|
| 588 |
+
}
|
SQL_s.docx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c12f86dcdfeed5535a737ba9085c0c24d58920b41d603a3e7db81a72e83476ff
|
| 3 |
+
size 368494
|
__pycache__/app.cpython-311.pyc
ADDED
|
Binary file (2.03 kB). View file
|
|
|
__pycache__/app.cpython-312.pyc
ADDED
|
Binary file (1.93 kB). View file
|
|
|
__pycache__/help.cpython-311.pyc
ADDED
|
Binary file (8.65 kB). View file
|
|
|
__pycache__/help.cpython-312.pyc
ADDED
|
Binary file (1.97 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# from PIL import Image
|
| 3 |
+
# import help
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# st.set_page_config(page_title="Multimodal Chat", page_icon="🤖")
|
| 7 |
+
|
| 8 |
+
# st.title(" Multimodal Chat")
|
| 9 |
+
|
| 10 |
+
# # -------------------------
|
| 11 |
+
# # User Inputs
|
| 12 |
+
# # -------------------------
|
| 13 |
+
# text_input = st.text_area(
|
| 14 |
+
# "Enter your question (Optional)",
|
| 15 |
+
# placeholder="Ask something..."
|
| 16 |
+
# )
|
| 17 |
+
|
| 18 |
+
# uploaded_image = st.file_uploader(
|
| 19 |
+
# "Upload an image (Optional)",
|
| 20 |
+
# type=["png", "jpg", "jpeg"]
|
| 21 |
+
# )
|
| 22 |
+
|
| 23 |
+
# image = None
|
| 24 |
+
|
| 25 |
+
# if uploaded_image is not None:
|
| 26 |
+
# image = Image.open(uploaded_image)
|
| 27 |
+
# st.image(image, caption="Uploaded Image", width="stretch")
|
| 28 |
+
|
| 29 |
+
# # -------------------------
|
| 30 |
+
# # Submit Button
|
| 31 |
+
# # -------------------------
|
| 32 |
+
# if st.button("Submit"):
|
| 33 |
+
|
| 34 |
+
# # Check if at least one input is provided
|
| 35 |
+
# if not text_input and image is None:
|
| 36 |
+
# st.warning("Please provide either a text question or an image.")
|
| 37 |
+
# st.stop()
|
| 38 |
+
|
| 39 |
+
# # -------------------------
|
| 40 |
+
# # Case 1 : Text + Image
|
| 41 |
+
# # -------------------------
|
| 42 |
+
# if text_input and image is not None:
|
| 43 |
+
# st.success("Received both Text and Image")
|
| 44 |
+
|
| 45 |
+
# # Replace this with your model
|
| 46 |
+
# response = f"""
|
| 47 |
+
# Text:
|
| 48 |
+
# {text_input}
|
| 49 |
+
|
| 50 |
+
# Image received successfully.
|
| 51 |
+
|
| 52 |
+
# (Call your VLM here.)
|
| 53 |
+
# """
|
| 54 |
+
|
| 55 |
+
# # -------------------------
|
| 56 |
+
# # Case 2 : Only Text
|
| 57 |
+
# # -------------------------
|
| 58 |
+
# elif text_input:
|
| 59 |
+
# st.success("Received only Text")
|
| 60 |
+
|
| 61 |
+
# # Replace this with your LLM
|
| 62 |
+
# response = help.inital_step(text_input)
|
| 63 |
+
|
| 64 |
+
# # -------------------------
|
| 65 |
+
# # Case 3 : Only Image
|
| 66 |
+
# # -------------------------
|
| 67 |
+
# else:
|
| 68 |
+
# st.success("Received only Image")
|
| 69 |
+
|
| 70 |
+
# # Replace this with your Vision Model
|
| 71 |
+
|
| 72 |
+
# import tempfile
|
| 73 |
+
|
| 74 |
+
# if uploaded_image is not None:
|
| 75 |
+
|
| 76 |
+
# with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
| 77 |
+
# tmp.write(uploaded_image.getbuffer())
|
| 78 |
+
# temp_path = tmp.name
|
| 79 |
+
|
| 80 |
+
# response = help.image_input(temp_path)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# st.subheader("Response")
|
| 85 |
+
# st.write(response)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
import streamlit as st
|
| 92 |
+
from PIL import Image
|
| 93 |
+
import tempfile
|
| 94 |
+
import help
|
| 95 |
+
|
| 96 |
+
st.set_page_config(page_title="Error Assistant", page_icon="🤖")
|
| 97 |
+
|
| 98 |
+
st.title("🤖 SQL / Excel Error Assistant")
|
| 99 |
+
|
| 100 |
+
# ----------------------------------------
|
| 101 |
+
# Select Task
|
| 102 |
+
# ----------------------------------------
|
| 103 |
+
task = st.selectbox(
|
| 104 |
+
"Choose Task",
|
| 105 |
+
[
|
| 106 |
+
"SQL Error",
|
| 107 |
+
"Excel Error"
|
| 108 |
+
]
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# ----------------------------------------
|
| 112 |
+
# User Input
|
| 113 |
+
# ----------------------------------------
|
| 114 |
+
text_input = st.text_area(
|
| 115 |
+
"Enter your question (Optional)",
|
| 116 |
+
placeholder="Ask something..."
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
uploaded_image = st.file_uploader(
|
| 120 |
+
"Upload Screenshot (Optional)",
|
| 121 |
+
type=["png", "jpg", "jpeg"]
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
image = None
|
| 125 |
+
|
| 126 |
+
if uploaded_image:
|
| 127 |
+
image = Image.open(uploaded_image)
|
| 128 |
+
st.image(image, caption="Uploaded Image", width="stretch")
|
| 129 |
+
|
| 130 |
+
# ----------------------------------------
|
| 131 |
+
# Submit
|
| 132 |
+
# ----------------------------------------
|
| 133 |
+
if st.button("Submit"):
|
| 134 |
+
|
| 135 |
+
if not text_input and image is None:
|
| 136 |
+
st.warning("Please provide text or image.")
|
| 137 |
+
st.stop()
|
| 138 |
+
|
| 139 |
+
# ==========================
|
| 140 |
+
# SQL
|
| 141 |
+
# ==========================
|
| 142 |
+
if task == "SQL Error":
|
| 143 |
+
|
| 144 |
+
if text_input and image:
|
| 145 |
+
st.success("Received both Text and Image")
|
| 146 |
+
|
| 147 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
| 148 |
+
tmp.write(uploaded_image.getbuffer())
|
| 149 |
+
img_path = tmp.name
|
| 150 |
+
|
| 151 |
+
response = help.sql_text_image(text_input, img_path)
|
| 152 |
+
|
| 153 |
+
elif text_input:
|
| 154 |
+
st.success("Received only Text")
|
| 155 |
+
response = help.sql_text(text_input)
|
| 156 |
+
|
| 157 |
+
else:
|
| 158 |
+
st.success("Received only Image")
|
| 159 |
+
|
| 160 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
| 161 |
+
tmp.write(uploaded_image.getbuffer())
|
| 162 |
+
img_path = tmp.name
|
| 163 |
+
|
| 164 |
+
response = help.sql_image(img_path)
|
| 165 |
+
|
| 166 |
+
# ==========================
|
| 167 |
+
# Excel
|
| 168 |
+
# ==========================
|
| 169 |
+
else:
|
| 170 |
+
|
| 171 |
+
if text_input and image:
|
| 172 |
+
st.success("Received both Text and Image")
|
| 173 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
| 174 |
+
tmp.write(uploaded_image.getbuffer())
|
| 175 |
+
img_path = tmp.name
|
| 176 |
+
|
| 177 |
+
response = help.excel_text_image(text_input, img_path)
|
| 178 |
+
|
| 179 |
+
elif text_input:
|
| 180 |
+
st.success("Received only Text")
|
| 181 |
+
response = help.excel_text(text_input)
|
| 182 |
+
|
| 183 |
+
else:
|
| 184 |
+
st.success("Received only Image")
|
| 185 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
| 186 |
+
tmp.write(uploaded_image.getbuffer())
|
| 187 |
+
img_path = tmp.name
|
| 188 |
+
|
| 189 |
+
response = help.excel_image(img_path)
|
| 190 |
+
|
| 191 |
+
st.subheader("Response")
|
| 192 |
+
st.write(response)
|
faiss_index/index.faiss
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:40a5dbcae30c6bcb7dba6651b5dcdf33863dbb571025bb284687ed3de8ee411a
|
| 3 |
+
size 583725
|
faiss_index/index.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1b3267176abd0e48b42fde9793bdd528a2c0db2d4c5d65150bfdcf4234e878cc
|
| 3 |
+
size 105142
|
help.py
ADDED
|
@@ -0,0 +1,541 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from click import prompt
|
| 2 |
+
from langchain_core.runnables import RunnableParallel
|
| 3 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 4 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 5 |
+
from streamlit import text_input
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
+
from langchain_ollama import OllamaEmbeddings
|
| 8 |
+
from langchain_ollama import ChatOllama
|
| 9 |
+
from langchain_core.prompts import PromptTemplate
|
| 10 |
+
from ollama import chat
|
| 11 |
+
from langchain_core.runnables import RunnableParallel
|
| 12 |
+
|
| 13 |
+
#### ************************************************************************************SQL***********************************************************************************
|
| 14 |
+
|
| 15 |
+
def sql_text(text_input):
|
| 16 |
+
embedding = OllamaEmbeddings(model="nomic-embed-text")
|
| 17 |
+
|
| 18 |
+
vector_store = FAISS.load_local(
|
| 19 |
+
"faiss_index",
|
| 20 |
+
embedding,
|
| 21 |
+
allow_dangerous_deserialization=True
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
retriever = vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 1})
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
llm = ChatOllama(
|
| 28 |
+
model="llama3.2",
|
| 29 |
+
temperature=0
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
prompt = PromptTemplate(
|
| 33 |
+
template="""
|
| 34 |
+
You are an SQL assistant.
|
| 35 |
+
|
| 36 |
+
Use ONLY the provided context.
|
| 37 |
+
|
| 38 |
+
Rules:
|
| 39 |
+
1. If the SQL query exists in the context, return ONLY that SQL query.
|
| 40 |
+
2. Do not explain.
|
| 41 |
+
3. Do not rewrite the question.
|
| 42 |
+
4. Do not generate a new query.
|
| 43 |
+
5. If the SQL query is missing, reply exactly:
|
| 44 |
+
I don't know.
|
| 45 |
+
|
| 46 |
+
Context:
|
| 47 |
+
{context}
|
| 48 |
+
|
| 49 |
+
Question:
|
| 50 |
+
{question}
|
| 51 |
+
|
| 52 |
+
SQL:
|
| 53 |
+
""",
|
| 54 |
+
input_variables=["context", "question"],
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
parallel_chain = RunnableParallel({
|
| 59 |
+
'context': retriever ,
|
| 60 |
+
'question': RunnablePassthrough()
|
| 61 |
+
})
|
| 62 |
+
|
| 63 |
+
parser = StrOutputParser()
|
| 64 |
+
|
| 65 |
+
main_chain = parallel_chain | prompt | llm | parser
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
response = main_chain.invoke(text_input)
|
| 69 |
+
return response
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# def image_input( image_path):
|
| 77 |
+
|
| 78 |
+
# system_prompt = """
|
| 79 |
+
# Act as an expert OCR and data extraction assistant.
|
| 80 |
+
|
| 81 |
+
# Please format your response clearly using the following headings:
|
| 82 |
+
|
| 83 |
+
# ### User Statement
|
| 84 |
+
# Extract the introductory text written by the user at the very top of the image.
|
| 85 |
+
|
| 86 |
+
# ### Question Description
|
| 87 |
+
# Extract the problem statement from the left-hand panel under the Description tab.
|
| 88 |
+
|
| 89 |
+
# ### SQL Query
|
| 90 |
+
# Extract the exact SQL code written inside the dark-themed code editor on the right.
|
| 91 |
+
|
| 92 |
+
# ### Error Message
|
| 93 |
+
# Extract the red error message displayed at the bottom right.
|
| 94 |
+
# """
|
| 95 |
+
|
| 96 |
+
# response = chat(
|
| 97 |
+
# model="qwen2.5vl:7b",
|
| 98 |
+
# messages=[
|
| 99 |
+
# {
|
| 100 |
+
# "role": "system",
|
| 101 |
+
# "content": system_prompt
|
| 102 |
+
# },
|
| 103 |
+
# {
|
| 104 |
+
# "role": "user",
|
| 105 |
+
# "content": "Extract all requested information from this image.",
|
| 106 |
+
# "images": ["sql_eorror.png"]
|
| 107 |
+
# }
|
| 108 |
+
# ]
|
| 109 |
+
# )
|
| 110 |
+
|
| 111 |
+
# response_image_test =response.message.content
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# llm = ChatOllama(
|
| 115 |
+
# model="llama3.2",
|
| 116 |
+
# temperature=0
|
| 117 |
+
# )
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
# embedding = OllamaEmbeddings(model="nomic-embed-text")
|
| 121 |
+
|
| 122 |
+
# vector_store = FAISS.load_local(
|
| 123 |
+
# "faiss_index",
|
| 124 |
+
# embedding,
|
| 125 |
+
# allow_dangerous_deserialization=True
|
| 126 |
+
# )
|
| 127 |
+
|
| 128 |
+
# retriever = vector_store.as_retriever(
|
| 129 |
+
# search_type="similarity",
|
| 130 |
+
# search_kwargs={"k": 1}
|
| 131 |
+
# )
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
# prompt = PromptTemplate(
|
| 137 |
+
# template="""
|
| 138 |
+
# You are an SQL assistant.
|
| 139 |
+
|
| 140 |
+
# Use ONLY the provided context.
|
| 141 |
+
|
| 142 |
+
# Rules:
|
| 143 |
+
# 1. If the SQL query exists in the context, return ONLY that SQL query.
|
| 144 |
+
# 2. Do not explain.
|
| 145 |
+
# 3. Do not rewrite the question.
|
| 146 |
+
# 4. Do not generate a new query.
|
| 147 |
+
# 5. If the SQL query is missing, reply exactly:
|
| 148 |
+
# I don't know.
|
| 149 |
+
|
| 150 |
+
# Context:
|
| 151 |
+
# {context}
|
| 152 |
+
|
| 153 |
+
# Question:
|
| 154 |
+
# {question}
|
| 155 |
+
|
| 156 |
+
# SQL:
|
| 157 |
+
# """,
|
| 158 |
+
# input_variables=["context", "question"],
|
| 159 |
+
# )
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
# parallel_chain = RunnableParallel({
|
| 163 |
+
# 'context': retriever ,
|
| 164 |
+
# 'question': RunnablePassthrough()
|
| 165 |
+
# })
|
| 166 |
+
|
| 167 |
+
# parser = StrOutputParser()
|
| 168 |
+
|
| 169 |
+
# main_chain = parallel_chain | prompt | llm | parser
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
# response = main_chain.invoke(response_image_test)
|
| 173 |
+
# return response
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def sql_image(image_path):
|
| 179 |
+
|
| 180 |
+
# --------------------------------------------------
|
| 181 |
+
# STEP 1 : Extract ONLY the SQL question
|
| 182 |
+
# --------------------------------------------------
|
| 183 |
+
system_prompt = """
|
| 184 |
+
You are an OCR assistant.
|
| 185 |
+
|
| 186 |
+
Your job is to read the screenshot and extract ONLY the SQL problem statement.
|
| 187 |
+
|
| 188 |
+
Rules:
|
| 189 |
+
- Ignore the SQL editor.
|
| 190 |
+
- Ignore the error message.
|
| 191 |
+
- Ignore buttons.
|
| 192 |
+
- Ignore the database output.
|
| 193 |
+
- Ignore everything except the Question Description.
|
| 194 |
+
|
| 195 |
+
Return ONLY the question text.
|
| 196 |
+
Do not add headings.
|
| 197 |
+
Do not explain anything.
|
| 198 |
+
"""
|
| 199 |
+
|
| 200 |
+
response = chat(
|
| 201 |
+
model="qwen2.5vl:7b",
|
| 202 |
+
messages=[
|
| 203 |
+
{
|
| 204 |
+
"role": "system",
|
| 205 |
+
"content": system_prompt
|
| 206 |
+
},
|
| 207 |
+
{
|
| 208 |
+
"role": "user",
|
| 209 |
+
"content": "Extract only the SQL question.",
|
| 210 |
+
"images": [image_path]
|
| 211 |
+
}
|
| 212 |
+
]
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
extracted_question = response.message.content.strip()
|
| 216 |
+
|
| 217 |
+
print("\nExtracted Question:\n")
|
| 218 |
+
print(extracted_question)
|
| 219 |
+
|
| 220 |
+
# --------------------------------------------------
|
| 221 |
+
# STEP 2 : Load LLM
|
| 222 |
+
# --------------------------------------------------
|
| 223 |
+
|
| 224 |
+
llm = ChatOllama(
|
| 225 |
+
model="llama3.2",
|
| 226 |
+
temperature=0
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
# --------------------------------------------------
|
| 230 |
+
# STEP 3 : Load Embeddings
|
| 231 |
+
# --------------------------------------------------
|
| 232 |
+
|
| 233 |
+
embedding = OllamaEmbeddings(
|
| 234 |
+
model="nomic-embed-text"
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
vector_store = FAISS.load_local(
|
| 238 |
+
"faiss_index",
|
| 239 |
+
embedding,
|
| 240 |
+
allow_dangerous_deserialization=True
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
retriever = vector_store.as_retriever(
|
| 244 |
+
search_type="similarity",
|
| 245 |
+
search_kwargs={"k":3}
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# --------------------------------------------------
|
| 249 |
+
# STEP 4 : Prompt
|
| 250 |
+
# --------------------------------------------------
|
| 251 |
+
|
| 252 |
+
prompt = PromptTemplate(
|
| 253 |
+
template="""
|
| 254 |
+
You are an SQL assistant.
|
| 255 |
+
|
| 256 |
+
Use ONLY the retrieved context.
|
| 257 |
+
|
| 258 |
+
Rules:
|
| 259 |
+
|
| 260 |
+
1. Return ONLY the SQL query.
|
| 261 |
+
2. No explanation.
|
| 262 |
+
3. No markdown.
|
| 263 |
+
4. Do not rewrite the question.
|
| 264 |
+
5. If the answer is not present in the context, reply exactly:
|
| 265 |
+
|
| 266 |
+
I don't know.
|
| 267 |
+
|
| 268 |
+
Context:
|
| 269 |
+
{context}
|
| 270 |
+
|
| 271 |
+
Question:
|
| 272 |
+
{question}
|
| 273 |
+
|
| 274 |
+
SQL:
|
| 275 |
+
""",
|
| 276 |
+
input_variables=["context", "question"],
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
# --------------------------------------------------
|
| 280 |
+
# STEP 5 : Chain
|
| 281 |
+
# --------------------------------------------------
|
| 282 |
+
|
| 283 |
+
parallel_chain = RunnableParallel(
|
| 284 |
+
{
|
| 285 |
+
"context": retriever,
|
| 286 |
+
"question": RunnablePassthrough()
|
| 287 |
+
}
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
parser = StrOutputParser()
|
| 291 |
+
|
| 292 |
+
main_chain = (
|
| 293 |
+
parallel_chain
|
| 294 |
+
| prompt
|
| 295 |
+
| llm
|
| 296 |
+
| parser
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
# --------------------------------------------------
|
| 300 |
+
# STEP 6 : Retrieve using ONLY the extracted question
|
| 301 |
+
# --------------------------------------------------
|
| 302 |
+
|
| 303 |
+
response = main_chain.invoke(extracted_question)
|
| 304 |
+
|
| 305 |
+
return response
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
def sql_text_image(question, image_path):
|
| 312 |
+
# --------------------------------------------------
|
| 313 |
+
# STEP 1 : Extract ONLY the SQL question from the image
|
| 314 |
+
# --------------------------------------------------
|
| 315 |
+
sql_answer1 = sql_image(image_path)
|
| 316 |
+
sql_answer2 = sql_text(question)
|
| 317 |
+
|
| 318 |
+
# --------------------------------------------------
|
| 319 |
+
# STEP 2 : Combine the extracted question and the text input
|
| 320 |
+
# --------------------------------------------------
|
| 321 |
+
# Build the Parallel Chain
|
| 322 |
+
prompt = f"""
|
| 323 |
+
You are an SQL expert.
|
| 324 |
+
|
| 325 |
+
Image SQL:
|
| 326 |
+
{sql_answer1}
|
| 327 |
+
|
| 328 |
+
User Question:
|
| 329 |
+
{sql_answer2}
|
| 330 |
+
|
| 331 |
+
Use BOTH pieces of information to produce the final SQL answer.
|
| 332 |
+
|
| 333 |
+
Output exactly in this format:
|
| 334 |
+
|
| 335 |
+
Kindly go through the provided information.
|
| 336 |
+
<final_answer> -> By Prince Kushwaha
|
| 337 |
+
|
| 338 |
+
Do not include any explanation or markdown.
|
| 339 |
+
Replace <final_answer> with the actual SQL answer.
|
| 340 |
+
"""
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
llm = ChatOllama(
|
| 344 |
+
model="llama3.2",
|
| 345 |
+
temperature=0
|
| 346 |
+
)
|
| 347 |
+
response = llm.invoke(prompt)
|
| 348 |
+
|
| 349 |
+
return response.content
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
### *************************************************************************Excel**************************************************************************
|
| 356 |
+
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
def excel_text(question):
|
| 360 |
+
# Load LLM
|
| 361 |
+
llm = ChatOllama(
|
| 362 |
+
model="llama3.2",
|
| 363 |
+
temperature=0
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
+
# Prompt
|
| 367 |
+
prompt = PromptTemplate(
|
| 368 |
+
template="""
|
| 369 |
+
You are an Excel expert.
|
| 370 |
+
|
| 371 |
+
Answer the following Excel question.
|
| 372 |
+
|
| 373 |
+
Rules:
|
| 374 |
+
1. Return only the final answer.
|
| 375 |
+
2. If the answer is an Excel formula, return only the formula.
|
| 376 |
+
3. Do not use markdown.
|
| 377 |
+
4. Keep the answer concise.
|
| 378 |
+
|
| 379 |
+
Question:
|
| 380 |
+
{question}
|
| 381 |
+
|
| 382 |
+
Answer:
|
| 383 |
+
""",
|
| 384 |
+
input_variables=["question"],
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
+
# Output Parser
|
| 388 |
+
parser = StrOutputParser()
|
| 389 |
+
|
| 390 |
+
# Chain
|
| 391 |
+
main_chain = prompt | llm | parser
|
| 392 |
+
|
| 393 |
+
# Generate Response
|
| 394 |
+
response = main_chain.invoke({
|
| 395 |
+
"question": text_input
|
| 396 |
+
})
|
| 397 |
+
|
| 398 |
+
return response.content.strip()
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
def excel_image(image_path):
|
| 409 |
+
# --------------------------------------------------
|
| 410 |
+
# STEP 1 : Extract ONLY the Excel question
|
| 411 |
+
# --------------------------------------------------
|
| 412 |
+
|
| 413 |
+
system_prompt = """
|
| 414 |
+
You are an OCR assistant.
|
| 415 |
+
|
| 416 |
+
Your task is to read the screenshot and extract ONLY the Excel question.
|
| 417 |
+
|
| 418 |
+
Rules:
|
| 419 |
+
- Ignore the Excel ribbon.
|
| 420 |
+
- Ignore row and column headers.
|
| 421 |
+
- Ignore formulas already written.
|
| 422 |
+
- Ignore buttons and menus.
|
| 423 |
+
- Ignore answer choices if not part of the question.
|
| 424 |
+
- Return ONLY the question text.
|
| 425 |
+
|
| 426 |
+
Do not explain anything.
|
| 427 |
+
Do not add headings.
|
| 428 |
+
"""
|
| 429 |
+
|
| 430 |
+
response = chat(
|
| 431 |
+
model="qwen2.5vl:7b",
|
| 432 |
+
messages=[
|
| 433 |
+
{
|
| 434 |
+
"role": "system",
|
| 435 |
+
"content": system_prompt
|
| 436 |
+
},
|
| 437 |
+
{
|
| 438 |
+
"role": "user",
|
| 439 |
+
"content": "Extract only the Excel question.",
|
| 440 |
+
"images": [image_path]
|
| 441 |
+
}
|
| 442 |
+
]
|
| 443 |
+
)
|
| 444 |
+
|
| 445 |
+
extracted_question = response.message.content.strip()
|
| 446 |
+
|
| 447 |
+
print("\nExtracted Question:\n")
|
| 448 |
+
print(extracted_question)
|
| 449 |
+
|
| 450 |
+
# --------------------------------------------------
|
| 451 |
+
# STEP 2 : Load LLM
|
| 452 |
+
# --------------------------------------------------
|
| 453 |
+
|
| 454 |
+
llm = ChatOllama(
|
| 455 |
+
model="llama3.2",
|
| 456 |
+
temperature=0
|
| 457 |
+
)
|
| 458 |
+
|
| 459 |
+
# --------------------------------------------------
|
| 460 |
+
# STEP 3 : Prompt
|
| 461 |
+
# --------------------------------------------------
|
| 462 |
+
|
| 463 |
+
prompt = PromptTemplate(
|
| 464 |
+
template="""
|
| 465 |
+
You are an Excel expert.
|
| 466 |
+
|
| 467 |
+
Answer the following Excel question.
|
| 468 |
+
|
| 469 |
+
Rules:
|
| 470 |
+
1. Provide the correct answer only.
|
| 471 |
+
2. If a formula is required, return only the formula.
|
| 472 |
+
3. If an explanation is required, keep it concise.
|
| 473 |
+
4. Do not use markdown.
|
| 474 |
+
|
| 475 |
+
Question:
|
| 476 |
+
{question}
|
| 477 |
+
|
| 478 |
+
Answer:
|
| 479 |
+
""",
|
| 480 |
+
input_variables=["question"],
|
| 481 |
+
)
|
| 482 |
+
|
| 483 |
+
parser = StrOutputParser()
|
| 484 |
+
|
| 485 |
+
chain = prompt | llm | parser
|
| 486 |
+
|
| 487 |
+
# --------------------------------------------------
|
| 488 |
+
# STEP 4 : Generate Answer
|
| 489 |
+
# --------------------------------------------------
|
| 490 |
+
|
| 491 |
+
answer = chain.invoke({
|
| 492 |
+
"question": extracted_question
|
| 493 |
+
})
|
| 494 |
+
|
| 495 |
+
return answer
|
| 496 |
+
|
| 497 |
+
def excel_text_image(question, image_path):
|
| 498 |
+
"""
|
| 499 |
+
question : Extracted text from OCR
|
| 500 |
+
image_path : Path to the Excel screenshot (optional, not used here)
|
| 501 |
+
"""
|
| 502 |
+
|
| 503 |
+
# Load LLM
|
| 504 |
+
llm = ChatOllama(
|
| 505 |
+
model="llama3.2",
|
| 506 |
+
temperature=0
|
| 507 |
+
)
|
| 508 |
+
|
| 509 |
+
# Prompt
|
| 510 |
+
prompt = PromptTemplate(
|
| 511 |
+
template="""
|
| 512 |
+
You are an Excel expert.
|
| 513 |
+
|
| 514 |
+
Answer the following Excel question.
|
| 515 |
+
|
| 516 |
+
Rules:
|
| 517 |
+
1. Return only the final answer.
|
| 518 |
+
2. If the answer is an Excel formula, return only the formula.
|
| 519 |
+
3. If the question asks for a value, return only the value.
|
| 520 |
+
4. If an explanation is needed, keep it brief.
|
| 521 |
+
5. Do not use markdown.
|
| 522 |
+
|
| 523 |
+
Question:
|
| 524 |
+
{question}
|
| 525 |
+
|
| 526 |
+
Answer:
|
| 527 |
+
""",
|
| 528 |
+
input_variables=["question"],
|
| 529 |
+
)
|
| 530 |
+
|
| 531 |
+
parser = StrOutputParser()
|
| 532 |
+
|
| 533 |
+
# Chain
|
| 534 |
+
chain = prompt | llm | parser
|
| 535 |
+
|
| 536 |
+
# Generate answer
|
| 537 |
+
response = chain.invoke({
|
| 538 |
+
"question": question
|
| 539 |
+
})
|
| 540 |
+
|
| 541 |
+
return response
|
sql_eorror.png
ADDED
|