Spaces:
Running
Running
Update prompt/prompt_services.py
Browse files- prompt/prompt_services.py +10 -6
prompt/prompt_services.py
CHANGED
|
@@ -3,15 +3,18 @@ import openai, os, json, re
|
|
| 3 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 4 |
sys_prompt = os.getenv("SYS_PROMPT")
|
| 5 |
|
| 6 |
-
def get_prompts(image_base64, category, user_prompt, sentiment, negative_prompt):
|
| 7 |
try:
|
|
|
|
|
|
|
|
|
|
| 8 |
if negative_prompt:
|
| 9 |
message = [
|
| 10 |
{
|
| 11 |
"role": "system",
|
| 12 |
"content": f"""{sys_prompt}
|
| 13 |
-
Return only a JSON with
|
| 14 |
-
Respond in this JSON format:\n{
|
| 15 |
},
|
| 16 |
{
|
| 17 |
"role": "user",
|
|
@@ -29,8 +32,8 @@ def get_prompts(image_base64, category, user_prompt, sentiment, negative_prompt)
|
|
| 29 |
{
|
| 30 |
"role": "system",
|
| 31 |
"content": f"""{sys_prompt}
|
| 32 |
-
Return only a JSON with
|
| 33 |
-
Respond in this JSON format:\n{
|
| 34 |
},
|
| 35 |
{
|
| 36 |
"role": "user",
|
|
@@ -50,6 +53,7 @@ def get_prompts(image_base64, category, user_prompt, sentiment, negative_prompt)
|
|
| 50 |
)
|
| 51 |
content = response.choices[0].message.content.strip()
|
| 52 |
content = re.sub(r"^```json\s*|\s*```$", "", content)
|
|
|
|
| 53 |
return json.loads(content)["variations"]
|
| 54 |
except Exception:
|
| 55 |
-
return []
|
|
|
|
| 3 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 4 |
sys_prompt = os.getenv("SYS_PROMPT")
|
| 5 |
|
| 6 |
+
def get_prompts(image_base64, category, user_prompt, sentiment, negative_prompt, num_variations):
|
| 7 |
try:
|
| 8 |
+
prompt_examples = [f"Prompt {i + 1}" for i in range(num_variations)]
|
| 9 |
+
json_format_example = json.dumps({"variations": prompt_examples})
|
| 10 |
+
|
| 11 |
if negative_prompt:
|
| 12 |
message = [
|
| 13 |
{
|
| 14 |
"role": "system",
|
| 15 |
"content": f"""{sys_prompt}
|
| 16 |
+
Return only a JSON with {num_variations} variations of ad prompts for image generation, based on the input image.
|
| 17 |
+
Respond in this JSON format:\n{json_format_example}"""
|
| 18 |
},
|
| 19 |
{
|
| 20 |
"role": "user",
|
|
|
|
| 32 |
{
|
| 33 |
"role": "system",
|
| 34 |
"content": f"""{sys_prompt}
|
| 35 |
+
Return only a JSON with {num_variations} variations of ad prompts for image generation, based on the input image.
|
| 36 |
+
Respond in this JSON format:\n{json_format_example}"""
|
| 37 |
},
|
| 38 |
{
|
| 39 |
"role": "user",
|
|
|
|
| 53 |
)
|
| 54 |
content = response.choices[0].message.content.strip()
|
| 55 |
content = re.sub(r"^```json\s*|\s*```$", "", content)
|
| 56 |
+
print(f"Prompt variations response: {content}")
|
| 57 |
return json.loads(content)["variations"]
|
| 58 |
except Exception:
|
| 59 |
+
return []
|