Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -57,39 +57,62 @@ class PromptRefiner:
|
|
57 |
temperature=0.7
|
58 |
)
|
59 |
response_content = response.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
try:
|
61 |
-
#
|
62 |
json_match = re.search(r'<json>\s*(.*?)\s*</json>', response_content, re.DOTALL)
|
63 |
if json_match:
|
64 |
json_str = json_match.group(1)
|
65 |
-
# Remove newlines and escape quotes within the JSON string
|
66 |
json_str = re.sub(r'\n\s*', ' ', json_str)
|
67 |
json_str = json_str.replace('"', '\\"')
|
68 |
-
# Wrap the entire string in quotes and parse it
|
69 |
json_output = json.loads(f'"{json_str}"')
|
70 |
-
|
71 |
if isinstance(json_output, str):
|
72 |
json_output = json.loads(json_output)
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
print(f"Raw content: {response_content}")
|
83 |
-
# If JSON parsing fails, attempt to extract the content manually
|
84 |
output = {}
|
85 |
for key in ["initial_prompt_evaluation", "refined_prompt", "explanation_of_refinements"]:
|
86 |
pattern = rf'"{key}":\s*"(.*?)"(?:,|\}})'
|
87 |
match = re.search(pattern, response_content, re.DOTALL)
|
88 |
-
if match
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
def apply_prompt(self, prompt: str, model: str) -> str:
|
95 |
try:
|
@@ -200,7 +223,6 @@ class PromptRefiner:
|
|
200 |
"content": prompt
|
201 |
}
|
202 |
]'''
|
203 |
-
|
204 |
response = self.client.chat.completions.create(
|
205 |
model=model,
|
206 |
messages=messages,
|
@@ -210,8 +232,8 @@ class PromptRefiner:
|
|
210 |
|
211 |
output = response.choices[0].message.content.strip()
|
212 |
# Basic post-processing
|
213 |
-
|
214 |
-
|
215 |
except Exception as e:
|
216 |
return f"Error: {str(e)}"
|
217 |
|
|
|
57 |
temperature=0.7
|
58 |
)
|
59 |
response_content = response.choices[0].message.content.strip()
|
60 |
+
|
61 |
+
# Parse the response
|
62 |
+
result = self._parse_response(response_content)
|
63 |
+
|
64 |
+
return (
|
65 |
+
result.get('initial_prompt_evaluation', ''),
|
66 |
+
result.get('refined_prompt', ''),
|
67 |
+
result.get('explanation_of_refinements', ''),
|
68 |
+
result
|
69 |
+
)
|
70 |
+
|
71 |
+
except Exception as e:
|
72 |
+
return (
|
73 |
+
f"Error: {str(e)}",
|
74 |
+
"",
|
75 |
+
"An unexpected error occurred.",
|
76 |
+
{}
|
77 |
+
)
|
78 |
+
def _parse_response(self, response_content: str) -> dict:
|
79 |
try:
|
80 |
+
# Try to find JSON in response
|
81 |
json_match = re.search(r'<json>\s*(.*?)\s*</json>', response_content, re.DOTALL)
|
82 |
if json_match:
|
83 |
json_str = json_match.group(1)
|
|
|
84 |
json_str = re.sub(r'\n\s*', ' ', json_str)
|
85 |
json_str = json_str.replace('"', '\\"')
|
|
|
86 |
json_output = json.loads(f'"{json_str}"')
|
87 |
+
|
88 |
if isinstance(json_output, str):
|
89 |
json_output = json.loads(json_output)
|
90 |
+
output={
|
91 |
+
key: value.replace('\\"', '"') if isinstance(value, str) else value
|
92 |
+
for key, value in json_output.items()
|
93 |
+
}
|
94 |
+
output['response_content']=json_output
|
95 |
+
# Clean up JSON values
|
96 |
+
return output
|
97 |
+
|
98 |
+
# Fallback to regex parsing if no JSON found
|
|
|
|
|
99 |
output = {}
|
100 |
for key in ["initial_prompt_evaluation", "refined_prompt", "explanation_of_refinements"]:
|
101 |
pattern = rf'"{key}":\s*"(.*?)"(?:,|\}})'
|
102 |
match = re.search(pattern, response_content, re.DOTALL)
|
103 |
+
output[key] = match.group(1).replace('\\n', '\n').replace('\\"', '"') if match else ""
|
104 |
+
output['response_content']=response_content
|
105 |
+
return output
|
106 |
+
|
107 |
+
except (json.JSONDecodeError, ValueError) as e:
|
108 |
+
print(f"Error parsing response: {e}")
|
109 |
+
print(f"Raw content: {response_content}")
|
110 |
+
return {
|
111 |
+
"initial_prompt_evaluation": "Error parsing response",
|
112 |
+
"refined_prompt": "",
|
113 |
+
"explanation_of_refinements": str(e),
|
114 |
+
'response_content':str(e)
|
115 |
+
}
|
116 |
|
117 |
def apply_prompt(self, prompt: str, model: str) -> str:
|
118 |
try:
|
|
|
223 |
"content": prompt
|
224 |
}
|
225 |
]'''
|
|
|
226 |
response = self.client.chat.completions.create(
|
227 |
model=model,
|
228 |
messages=messages,
|
|
|
232 |
|
233 |
output = response.choices[0].message.content.strip()
|
234 |
# Basic post-processing
|
235 |
+
return output.replace('\n\n', '\n').strip()
|
236 |
+
|
237 |
except Exception as e:
|
238 |
return f"Error: {str(e)}"
|
239 |
|