jean commited on
Commit
ddd3e5e
·
1 Parent(s): 3eaa3b6

fix prompt bug

Browse files
.env CHANGED
@@ -1,4 +1,4 @@
1
  LANGSMITH_TRACING=true
2
  LANGSMITH_ENDPOINT="https://api.smith.langchain.com"
3
  LANGSMITH_API_KEY="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"
4
- LANGSMITH_PROJECT="pr-internal-neglect-42"
 
1
  LANGSMITH_TRACING=true
2
  LANGSMITH_ENDPOINT="https://api.smith.langchain.com"
3
  LANGSMITH_API_KEY="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"
4
+ LANGSMITH_PROJECT="pr-internal-neglect-42".
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  from services.analyse_image import ErrorResponse, analyze_image
3
- from utils.utils import image_to_base64_data_uri, read_image_as_data_url
4
  from dotenv import load_dotenv
 
5
 
6
  load_dotenv()
7
 
@@ -94,7 +94,7 @@ with gr.Blocks() as demo:
94
  fn=upload_and_analyse,
95
  inputs=image_input,
96
  outputs=[car_block, animal_block, flower_block, status],
97
- show_progress=False # 🛠️ Important when using async fn
98
  )
99
 
100
  demo.launch()
 
1
  import gradio as gr
2
  from services.analyse_image import ErrorResponse, analyze_image
 
3
  from dotenv import load_dotenv
4
+ from utils.utils import image_to_base64_data_uri
5
 
6
  load_dotenv()
7
 
 
94
  fn=upload_and_analyse,
95
  inputs=image_input,
96
  outputs=[car_block, animal_block, flower_block, status],
97
+ show_progress=True # 🛠️ Important when using async fn
98
  )
99
 
100
  demo.launch()
llms/__pycache__/qwen.cpython-311.pyc CHANGED
Binary files a/llms/__pycache__/qwen.cpython-311.pyc and b/llms/__pycache__/qwen.cpython-311.pyc differ
 
prompt/__pycache__/object_prompt.cpython-311.pyc CHANGED
Binary files a/prompt/__pycache__/object_prompt.cpython-311.pyc and b/prompt/__pycache__/object_prompt.cpython-311.pyc differ
 
prompt/object_prompt.py CHANGED
@@ -3,8 +3,7 @@ from langchain_core.messages import SystemMessage, HumanMessage, BaseMessage
3
  from pydantic import BaseModel
4
  from langchain.output_parsers import PydanticOutputParser
5
  from models.structured_model import StructuredModel
6
-
7
-
8
  from typing import Literal, TypedDict, Union, List
9
 
10
 
@@ -20,30 +19,32 @@ ChatMessages = List[ChatMessage]
20
 
21
 
22
 
23
- def get_system_intructions(structure: StructuredModel):
24
- parser = PydanticOutputParser(pydantic_object=structure)
25
- return parser.get_format_instructions()
26
 
27
- def convert_langchain_messages(messages: List[BaseMessage]) -> ChatMessages:
28
- return [{"role": m.type, "content": m.content} for m in messages]
29
 
30
 
31
  def get_prompt_template(label: str, image_data_url: str, structure: StructuredModel):
32
-
33
- print(get_system_intructions(structure))
34
-
35
-
36
- return convert_langchain_messages([
37
- SystemMessage(content=(
38
- "You are a vision-language model specialized in analyzing objects in images. "
39
- "If you don't know the value of an attribute asked to be extracted, return null. "
40
- f"The object in the image is {label}."
41
- f"{get_system_intructions(structure)}"
42
- )),
43
- HumanMessage(content=[
44
- {"type": "image_url", "image_url": {"url": image_data_url}}
45
- ])
46
- ])
 
 
47
 
48
 
49
 
 
3
  from pydantic import BaseModel
4
  from langchain.output_parsers import PydanticOutputParser
5
  from models.structured_model import StructuredModel
6
+ from langchain.prompts import PromptTemplate
 
7
  from typing import Literal, TypedDict, Union, List
8
 
9
 
 
19
 
20
 
21
 
22
+ # def get_system_intructions(structure: StructuredModel):
23
+ # parser = PydanticOutputParser(pydantic_object=structure)
24
+ # return parser.get_format_instructions()
25
 
26
+ # def convert_langchain_messages(messages: List[BaseMessage]) -> ChatMessages:
27
+ # return [{"role": m.type, "content": m.content} for m in messages]
28
 
29
 
30
  def get_prompt_template(label: str, image_data_url: str, structure: StructuredModel):
31
+ parser = PydanticOutputParser(pydantic_object=structure)
32
+ prompt = PromptTemplate(
33
+ template="Answer the user query.\n{format_instructions}\n{query}\n",
34
+ input_variables=["query"],
35
+ partial_variables={"format_instructions": parser.get_format_instructions()},
36
+ )
37
+ prompt = prompt.format(query=f"Describe the {label}")
38
+ messages = [
39
+ {
40
+ "role": "user",
41
+ "content": [
42
+ {"type" : "text", "text": prompt},
43
+ {"type": "image_url", "image_url": {"url": image_data_url } }
44
+ ]
45
+ }
46
+ ]
47
+ return messages
48
 
49
 
50
 
pyproject.toml CHANGED
@@ -16,6 +16,7 @@ dependencies = [
16
  "langchainhub>=0.1.21",
17
  "langgraph>=0.5.4",
18
  "llama-cpp-python>=0.3.14",
 
19
  "python-multipart>=0.0.20",
20
  "text-generation>=0.7.0",
21
  "torch>=2.7.1",
 
16
  "langchainhub>=0.1.21",
17
  "langgraph>=0.5.4",
18
  "llama-cpp-python>=0.3.14",
19
+ "pillow>=11.3.0",
20
  "python-multipart>=0.0.20",
21
  "text-generation>=0.7.0",
22
  "torch>=2.7.1",
utils/__pycache__/utils.cpython-311.pyc CHANGED
Binary files a/utils/__pycache__/utils.cpython-311.pyc and b/utils/__pycache__/utils.cpython-311.pyc differ
 
utils/utils.py CHANGED
@@ -2,16 +2,6 @@ import base64
2
  import io
3
  from PIL import Image
4
 
5
- def encode_image_as_data_url(image_bytes: bytes, mime_type: str = "image/jpeg") -> str:
6
- image_b64 = base64.b64encode(image_bytes).decode("utf-8")
7
- return f"data:{mime_type};base64,{image_b64}"
8
-
9
-
10
- def read_image_as_data_url(image_path: str, mime_type: str = "image/jpeg") -> str:
11
- with open(image_path, "rb") as f:
12
- image_bytes = f.read()
13
- return encode_image_as_data_url(image_bytes, mime_type)
14
-
15
 
16
  def image_to_base64_data_uri(file_path, size=(224, 224)):
17
  # Open the image
 
2
  import io
3
  from PIL import Image
4
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def image_to_base64_data_uri(file_path, size=(224, 224)):
7
  # Open the image
uv.lock CHANGED
@@ -1357,6 +1357,7 @@ dependencies = [
1357
  { name = "langchainhub" },
1358
  { name = "langgraph" },
1359
  { name = "llama-cpp-python" },
 
1360
  { name = "python-multipart" },
1361
  { name = "text-generation" },
1362
  { name = "torch" },
@@ -1377,6 +1378,7 @@ requires-dist = [
1377
  { name = "langchainhub", specifier = ">=0.1.21" },
1378
  { name = "langgraph", specifier = ">=0.5.4" },
1379
  { name = "llama-cpp-python", specifier = ">=0.3.14", index = "https://abetlen.github.io/llama-cpp-python/whl/metal/" },
 
1380
  { name = "python-multipart", specifier = ">=0.0.20" },
1381
  { name = "text-generation", specifier = ">=0.7.0" },
1382
  { name = "torch", specifier = ">=2.7.1" },
 
1357
  { name = "langchainhub" },
1358
  { name = "langgraph" },
1359
  { name = "llama-cpp-python" },
1360
+ { name = "pillow" },
1361
  { name = "python-multipart" },
1362
  { name = "text-generation" },
1363
  { name = "torch" },
 
1378
  { name = "langchainhub", specifier = ">=0.1.21" },
1379
  { name = "langgraph", specifier = ">=0.5.4" },
1380
  { name = "llama-cpp-python", specifier = ">=0.3.14", index = "https://abetlen.github.io/llama-cpp-python/whl/metal/" },
1381
+ { name = "pillow", specifier = ">=11.3.0" },
1382
  { name = "python-multipart", specifier = ">=0.0.20" },
1383
  { name = "text-generation", specifier = ">=0.7.0" },
1384
  { name = "torch", specifier = ">=2.7.1" },