Spaces:
Runtime error
Runtime error
Commit ·
292d71a
1
Parent(s): 6577110
remove database saving or retrieving in prompt generator
Browse files- damo/services/llm/vision.py +46 -46
damo/services/llm/vision.py
CHANGED
|
@@ -13,52 +13,52 @@ client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
|
|
| 13 |
|
| 14 |
def generate_prompt_from_image(image_url, prompt, clothing_type, image_id=None, temperature=0.6):
|
| 15 |
material_obj = None
|
| 16 |
-
with Session(engine) as session:
|
| 17 |
-
if image_id:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
# new_material = Material(**{
|
| 63 |
# "name": image_id,
|
| 64 |
# "texture": material_obj["fabric_texture"],
|
|
@@ -74,6 +74,6 @@ def generate_prompt_from_image(image_url, prompt, clothing_type, image_id=None,
|
|
| 74 |
.replace("<other_clothing_attributes>", clothing_type.to_prompt()) \
|
| 75 |
.replace("<cloth_type>", clothing_type.name)
|
| 76 |
# .replace("<detailed_coloring_and_schema_description>", material_obj["color_and_scheme"]) \
|
| 77 |
-
session.commit()
|
| 78 |
-
session.close()
|
| 79 |
return prompt
|
|
|
|
| 13 |
|
| 14 |
def generate_prompt_from_image(image_url, prompt, clothing_type, image_id=None, temperature=0.6):
|
| 15 |
material_obj = None
|
| 16 |
+
# with Session(engine) as session:
|
| 17 |
+
# if image_id:
|
| 18 |
+
# material_obj = session.scalar(select(Material).where(Material.name.__eq__(image_id)))
|
| 19 |
+
if not material_obj:
|
| 20 |
+
fabric_analysis = client.chat.completions.create(
|
| 21 |
+
model="gpt-4-turbo",
|
| 22 |
+
response_format={"type": "json_object"},
|
| 23 |
+
messages=[
|
| 24 |
+
{
|
| 25 |
+
"role": "system",
|
| 26 |
+
"content": [
|
| 27 |
+
{"type": "text",
|
| 28 |
+
"text": "As a professional fabric analyst, analyze the material in the picture and fill in "
|
| 29 |
+
"the parameters in the user-provided template. Parameters are indicated as "
|
| 30 |
+
"<example_parameter>. Ensure the material description, and texture are precise. "
|
| 31 |
+
"Maintain the integrity of the prompt template, returning the response as a JSON "
|
| 32 |
+
"object with each parameter's value as a string. Do not alter the template values. "
|
| 33 |
+
"Your tone should be highly detailed and professional with short and concise "
|
| 34 |
+
"phrases without making sentences. "
|
| 35 |
|
| 36 |
+
}
|
| 37 |
+
]
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"role": "user",
|
| 41 |
+
"content": [
|
| 42 |
+
{"type": "text",
|
| 43 |
+
"text": prompt
|
| 44 |
|
| 45 |
+
}
|
| 46 |
+
]
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"role": "user",
|
| 50 |
+
"content": [
|
| 51 |
+
{"type": "image_url", "image_url": {"url": image_url}},
|
| 52 |
+
{"type": "text",
|
| 53 |
+
"text": f"Clothing type is: {clothing_type}, and other clothing attributes are: {clothing_type.to_prompt()}"}
|
| 54 |
+
]
|
| 55 |
+
}
|
| 56 |
+
],
|
| 57 |
+
temperature=temperature
|
| 58 |
+
)
|
| 59 |
+
dalle_prompt = fabric_analysis.choices[0].message.content
|
| 60 |
+
print(dalle_prompt)
|
| 61 |
+
material_obj = json.loads(dalle_prompt)
|
| 62 |
# new_material = Material(**{
|
| 63 |
# "name": image_id,
|
| 64 |
# "texture": material_obj["fabric_texture"],
|
|
|
|
| 74 |
.replace("<other_clothing_attributes>", clothing_type.to_prompt()) \
|
| 75 |
.replace("<cloth_type>", clothing_type.name)
|
| 76 |
# .replace("<detailed_coloring_and_schema_description>", material_obj["color_and_scheme"]) \
|
| 77 |
+
# session.commit()
|
| 78 |
+
# session.close()
|
| 79 |
return prompt
|