siddhartharya commited on
Commit
6c57b19
1 Parent(s): ba22d1b

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +21 -2
utils.py CHANGED
@@ -5,6 +5,8 @@ import os
5
  import tiktoken
6
  from gtts import gTTS
7
  import tempfile
 
 
8
 
9
  groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
10
  tokenizer = tiktoken.get_encoding("cl100k_base")
@@ -35,10 +37,27 @@ def generate_script(system_prompt: str, input_text: str, tone: str):
35
  temperature=0.7
36
  )
37
 
 
 
 
 
38
  try:
39
- dialogue = Dialogue.model_validate_json(response.choices[0].message.content)
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  except ValidationError as e:
41
- raise ValueError(f"Failed to parse dialogue JSON: {e}")
42
 
43
  return dialogue
44
 
 
5
  import tiktoken
6
  from gtts import gTTS
7
  import tempfile
8
+ import json
9
+ import re
10
 
11
  groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
12
  tokenizer = tiktoken.get_encoding("cl100k_base")
 
37
  temperature=0.7
38
  )
39
 
40
+ # Extract content and remove any markdown code block syntax
41
+ content = response.choices[0].message.content
42
+ content = re.sub(r'```json\s*|\s*```', '', content)
43
+
44
  try:
45
+ # First, try to parse as JSON
46
+ json_data = json.loads(content)
47
+ dialogue = Dialogue.model_validate(json_data)
48
+ except json.JSONDecodeError as json_error:
49
+ # If JSON parsing fails, try to extract JSON from the text
50
+ match = re.search(r'\{.*\}', content, re.DOTALL)
51
+ if match:
52
+ try:
53
+ json_data = json.loads(match.group())
54
+ dialogue = Dialogue.model_validate(json_data)
55
+ except (json.JSONDecodeError, ValidationError) as e:
56
+ raise ValueError(f"Failed to parse dialogue JSON: {e}\nContent: {content}")
57
+ else:
58
+ raise ValueError(f"Failed to find valid JSON in the response: {content}")
59
  except ValidationError as e:
60
+ raise ValueError(f"Failed to validate dialogue structure: {e}\nContent: {content}")
61
 
62
  return dialogue
63