Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -20,43 +20,59 @@ class RefinementOutput(BaseModel):
|
|
20 |
|
21 |
class PromptRefiner:
|
22 |
def __init__(self, api_token: str):
|
23 |
-
self.client = InferenceClient(token=api_token,timeout=300)
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
elif prompt_input.meta_prompt_choice == "star":
|
37 |
-
selected_meta_prompt = echo_prompt_refiner
|
38 |
-
elif prompt_input.meta_prompt_choice == "math":
|
39 |
-
selected_meta_prompt = math_meta_prompt
|
40 |
-
elif prompt_input.meta_prompt_choice == "arpe":
|
41 |
-
selected_meta_prompt = autoregressive_metaprompt
|
42 |
-
else:
|
43 |
-
selected_meta_prompt = advanced_meta_prompt
|
44 |
-
|
45 |
-
messages = [
|
46 |
-
{"role": "system", "content": 'You are an expert at refining and extending prompts. Given a basic prompt, provide a more detailed.'},
|
47 |
-
{"role": "user", "content": selected_meta_prompt.replace("[Insert initial prompt here]", prompt_input.text)}
|
48 |
-
]
|
49 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
response = self.client.chat_completion(
|
51 |
model=prompt_refiner_model,
|
52 |
messages=messages,
|
53 |
max_tokens=2000,
|
54 |
temperature=0.8
|
55 |
)
|
|
|
56 |
response_content = response.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
except HfHubHTTPError as e:
|
58 |
return (
|
59 |
-
"Error: Model timeout. Please again later.",
|
60 |
"",
|
61 |
"The selected model is currently experiencing high traffic.",
|
62 |
{}
|
@@ -68,49 +84,67 @@ class PromptRefiner:
|
|
68 |
"An unexpected error occurred.",
|
69 |
{}
|
70 |
)
|
|
|
|
|
71 |
try:
|
|
|
72 |
json_match = re.search(r'<json>\s*(.*?)\s*</json>', response_content, re.DOTALL)
|
73 |
if json_match:
|
74 |
json_str = json_match.group(1)
|
75 |
json_str = re.sub(r'\n\s*', ' ', json_str)
|
76 |
json_str = json_str.replace('"', '\\"')
|
77 |
json_output = json.loads(f'"{json_str}"')
|
|
|
78 |
if isinstance(json_output, str):
|
79 |
json_output = json.loads(json_output)
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
print(f"Raw content: {response_content}")
|
89 |
output = {}
|
90 |
for key in ["initial_prompt_evaluation", "refined_prompt", "explanation_of_refinements"]:
|
91 |
pattern = rf'"{key}":\s*"(.*?)"(?:,|\}})'
|
92 |
match = re.search(pattern, response_content, re.DOTALL)
|
93 |
-
if match
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
def apply_prompt(self, prompt: str, model: str) -> str:
|
100 |
try:
|
101 |
messages = [
|
102 |
-
{
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
]
|
|
|
105 |
response = self.client.chat_completion(
|
106 |
model=model,
|
107 |
messages=messages,
|
108 |
max_tokens=2000,
|
109 |
temperature=0.8
|
110 |
)
|
|
|
111 |
output = response.choices[0].message.content.strip()
|
112 |
-
|
113 |
-
|
114 |
except Exception as e:
|
115 |
return f"Error: {str(e)}"
|
116 |
|
|
|
20 |
|
21 |
class PromptRefiner:
|
22 |
def __init__(self, api_token: str):
|
23 |
+
self.client = InferenceClient(token=api_token, timeout=300)
|
24 |
+
self.meta_prompts = {
|
25 |
+
"morphosis": original_meta_prompt,
|
26 |
+
"verse": new_meta_prompt,
|
27 |
+
"physics": metaprompt1,
|
28 |
+
"bolism": loic_metaprompt,
|
29 |
+
"done": metadone,
|
30 |
+
"star": echo_prompt_refiner,
|
31 |
+
"math": math_meta_prompt,
|
32 |
+
"arpe": autoregressive_metaprompt
|
33 |
+
}
|
34 |
+
|
35 |
+
def refine_prompt(self, prompt_input: PromptInput) -> tuple:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
try:
|
37 |
+
# Select meta prompt using dictionary instead of if-elif chain
|
38 |
+
selected_meta_prompt = self.meta_prompts.get(
|
39 |
+
prompt_input.meta_prompt_choice,
|
40 |
+
advanced_meta_prompt
|
41 |
+
)
|
42 |
+
|
43 |
+
messages = [
|
44 |
+
{
|
45 |
+
"role": "system",
|
46 |
+
"content": 'You are an expert at refining and extending prompts. Given a basic prompt, provide a more detailed.'
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"role": "user",
|
50 |
+
"content": selected_meta_prompt.replace("[Insert initial prompt here]", prompt_input.text)
|
51 |
+
}
|
52 |
+
]
|
53 |
+
|
54 |
response = self.client.chat_completion(
|
55 |
model=prompt_refiner_model,
|
56 |
messages=messages,
|
57 |
max_tokens=2000,
|
58 |
temperature=0.8
|
59 |
)
|
60 |
+
|
61 |
response_content = response.choices[0].message.content.strip()
|
62 |
+
|
63 |
+
# Parse the response
|
64 |
+
result = self._parse_response(response_content)
|
65 |
+
|
66 |
+
return (
|
67 |
+
result.get('initial_prompt_evaluation', ''),
|
68 |
+
result.get('refined_prompt', ''),
|
69 |
+
result.get('explanation_of_refinements', ''),
|
70 |
+
result
|
71 |
+
)
|
72 |
+
|
73 |
except HfHubHTTPError as e:
|
74 |
return (
|
75 |
+
"Error: Model timeout. Please try again later.",
|
76 |
"",
|
77 |
"The selected model is currently experiencing high traffic.",
|
78 |
{}
|
|
|
84 |
"An unexpected error occurred.",
|
85 |
{}
|
86 |
)
|
87 |
+
|
88 |
+
def _parse_response(self, response_content: str) -> dict:
|
89 |
try:
|
90 |
+
# Try to find JSON in response
|
91 |
json_match = re.search(r'<json>\s*(.*?)\s*</json>', response_content, re.DOTALL)
|
92 |
if json_match:
|
93 |
json_str = json_match.group(1)
|
94 |
json_str = re.sub(r'\n\s*', ' ', json_str)
|
95 |
json_str = json_str.replace('"', '\\"')
|
96 |
json_output = json.loads(f'"{json_str}"')
|
97 |
+
|
98 |
if isinstance(json_output, str):
|
99 |
json_output = json.loads(json_output)
|
100 |
+
|
101 |
+
# Clean up JSON values
|
102 |
+
return {
|
103 |
+
key: value.replace('\\"', '"') if isinstance(value, str) else value
|
104 |
+
for key, value in json_output.items()
|
105 |
+
}
|
106 |
+
|
107 |
+
# Fallback to regex parsing if no JSON found
|
|
|
108 |
output = {}
|
109 |
for key in ["initial_prompt_evaluation", "refined_prompt", "explanation_of_refinements"]:
|
110 |
pattern = rf'"{key}":\s*"(.*?)"(?:,|\}})'
|
111 |
match = re.search(pattern, response_content, re.DOTALL)
|
112 |
+
output[key] = match.group(1).replace('\\n', '\n').replace('\\"', '"') if match else ""
|
113 |
+
|
114 |
+
return output
|
115 |
+
|
116 |
+
except (json.JSONDecodeError, ValueError) as e:
|
117 |
+
print(f"Error parsing response: {e}")
|
118 |
+
print(f"Raw content: {response_content}")
|
119 |
+
return {
|
120 |
+
"initial_prompt_evaluation": "Error parsing response",
|
121 |
+
"refined_prompt": "",
|
122 |
+
"explanation_of_refinements": str(e)
|
123 |
+
}
|
124 |
+
|
125 |
def apply_prompt(self, prompt: str, model: str) -> str:
|
126 |
try:
|
127 |
messages = [
|
128 |
+
{
|
129 |
+
"role": "system",
|
130 |
+
"content": "You are a helpful assistant. Answer in stylized version with latex format or markdown if relevant. Separate your answer into logical sections using level 2 headers (##) for sections and bolding (**) for subsections. Incorporate a variety of lists, headers, and text to make the answer visually appealing"
|
131 |
+
},
|
132 |
+
{
|
133 |
+
"role": "user",
|
134 |
+
"content": prompt
|
135 |
+
}
|
136 |
]
|
137 |
+
|
138 |
response = self.client.chat_completion(
|
139 |
model=model,
|
140 |
messages=messages,
|
141 |
max_tokens=2000,
|
142 |
temperature=0.8
|
143 |
)
|
144 |
+
|
145 |
output = response.choices[0].message.content.strip()
|
146 |
+
return output.replace('\n\n', '\n').strip()
|
147 |
+
|
148 |
except Exception as e:
|
149 |
return f"Error: {str(e)}"
|
150 |
|