yxmnjxzx commited on
Commit
b51fc11
·
verified ·
1 Parent(s): 13c25cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -36
app.py CHANGED
@@ -57,55 +57,39 @@ class PromptRefiner:
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
- def _parse_response(self, response_content: str) -> dict:
72
  try:
73
- # Try to find JSON in response
74
  json_match = re.search(r'<json>\s*(.*?)\s*</json>', response_content, re.DOTALL)
75
  if json_match:
76
  json_str = json_match.group(1)
 
77
  json_str = re.sub(r'\n\s*', ' ', json_str)
78
  json_str = json_str.replace('"', '\\"')
 
79
  json_output = json.loads(f'"{json_str}"')
80
-
81
  if isinstance(json_output, str):
82
  json_output = json.loads(json_output)
83
- output={
84
- key: value.replace('\\"', '"') if isinstance(value, str) else value
85
- for key, value in json_output.items()
86
- }
87
- output['response_content']=json_output
88
- # Clean up JSON values
89
- return output
90
-
91
- # Fallback to regex parsing if no JSON found
 
 
92
  output = {}
93
  for key in ["initial_prompt_evaluation", "refined_prompt", "explanation_of_refinements"]:
94
  pattern = rf'"{key}":\s*"(.*?)"(?:,|\}})'
95
  match = re.search(pattern, response_content, re.DOTALL)
96
- output[key] = match.group(1).replace('\\n', '\n').replace('\\"', '"') if match else ""
97
- output['response_content']=response_content
98
- return output
99
-
100
- except (json.JSONDecodeError, ValueError) as e:
101
- print(f"Error parsing response: {e}")
102
- print(f"Raw content: {response_content}")
103
- return {
104
- "initial_prompt_evaluation": "Error parsing response",
105
- "refined_prompt": "",
106
- "explanation_of_refinements": str(e),
107
- 'response_content':str(e)
108
- }
109
 
110
  def apply_prompt(self, prompt: str, model: str) -> str:
111
  try:
 
57
  temperature=0.7
58
  )
59
  response_content = response.choices[0].message.content.strip()
 
 
 
 
 
 
 
 
 
 
 
 
60
  try:
61
+ # Extract JSON from between <json> tags
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
+ # Ensure json_output is a dictionary
71
  if isinstance(json_output, str):
72
  json_output = json.loads(json_output)
73
+ # Unescape the parsed JSON
74
+ for key, value in json_output.items():
75
+ if isinstance(value, str):
76
+ json_output[key] = value.replace('\\"', '"')
77
+ return RefinementOutput(**json_output, raw_content=response_content)
78
+ else:
79
+ raise ValueError("No JSON found in the response")
80
+ except (json.JSONDecodeError, ValueError) as e:
81
+ print(f"Error parsing JSON: {e}")
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
+ output[key] = match.group(1).replace('\\n', '\n').replace('\\"', '"')
90
+ else:
91
+ output[key] = "" # Set empty string if content not found
92
+ return RefinementOutput(**output, raw_content=response_content)
 
 
 
 
 
 
 
 
93
 
94
  def apply_prompt(self, prompt: str, model: str) -> str:
95
  try: