Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,14 +8,34 @@ import re
|
|
8 |
API_URL = "https://deployment.datasaur.ai/api/deployment/8/2723/chat/completions"
|
9 |
API_TOKEN = os.environ["DATASAUR_API_KEY"]
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
if match:
|
16 |
-
return match.group(1)
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
def magic_function(input_text):
|
21 |
"""
|
@@ -26,7 +46,7 @@ def magic_function(input_text):
|
|
26 |
"Authorization": f"Bearer {API_TOKEN}",
|
27 |
}
|
28 |
data = {
|
29 |
-
"messages": [{"role": "user", "content": f"`{input_text}`"}]
|
30 |
}
|
31 |
|
32 |
try:
|
@@ -39,7 +59,7 @@ def magic_function(input_text):
|
|
39 |
# This may need adjustment if the API has a different format.
|
40 |
content = response_json.get("choices", [{}])[0].get("message", {}).get("content", "Error: Could not parse response.")
|
41 |
|
42 |
-
|
43 |
|
44 |
return content.strip()
|
45 |
|
|
|
8 |
API_URL = "https://deployment.datasaur.ai/api/deployment/8/2723/chat/completions"
|
9 |
API_TOKEN = os.environ["DATASAUR_API_KEY"]
|
10 |
|
11 |
+
import re
|
12 |
+
import json
|
13 |
+
|
14 |
+
def extract_suggestion_value(text):
|
15 |
+
"""
|
16 |
+
Extracts the value of the 'suggestion' key from a given string that may contain JSON-like content.
|
17 |
+
|
18 |
+
Parameters:
|
19 |
+
text (str): Input text containing a JSON-like 'suggestion' field.
|
20 |
+
|
21 |
+
Returns:
|
22 |
+
dict: Dictionary with the 'suggestion' key and its extracted value, or None if not found.
|
23 |
+
"""
|
24 |
+
# First, try to directly parse as JSON
|
25 |
+
try:
|
26 |
+
parsed = json.loads(text)
|
27 |
+
if "suggestion" in parsed:
|
28 |
+
return {"suggestion": parsed["suggestion"]}
|
29 |
+
except json.JSONDecodeError:
|
30 |
+
pass # Fall back to regex if not valid JSON
|
31 |
+
|
32 |
+
# Fallback: use regex to extract suggestion value
|
33 |
+
match = re.search(r'"suggestion"\s*:\s*"([^"]+)"', text)
|
34 |
if match:
|
35 |
+
return {"suggestion": match.group(1)}
|
36 |
+
|
37 |
+
return None
|
38 |
+
|
39 |
|
40 |
def magic_function(input_text):
|
41 |
"""
|
|
|
46 |
"Authorization": f"Bearer {API_TOKEN}",
|
47 |
}
|
48 |
data = {
|
49 |
+
"messages": [{"role": "user", "content": f"Input: `{input_text}`"}]
|
50 |
}
|
51 |
|
52 |
try:
|
|
|
59 |
# This may need adjustment if the API has a different format.
|
60 |
content = response_json.get("choices", [{}])[0].get("message", {}).get("content", "Error: Could not parse response.")
|
61 |
|
62 |
+
content = extract_suggestion_value(content)
|
63 |
|
64 |
return content.strip()
|
65 |
|