allbibek commited on
Commit
269f94b
·
verified ·
1 Parent(s): eb96b50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -15
app.py CHANGED
@@ -10,22 +10,51 @@ from docx import Document
10
 
11
  def call_model(client, model_name, system_prompt, user_prompt):
12
 
13
- res = client.chat.completions.create(
14
- model=model_name,
15
- temperature=0.2,
16
- messages=[
17
- {
18
- "role": "system",
19
- "content": system_prompt
20
- },
21
- {
22
- "role": "user",
23
- "content": user_prompt
24
- }
25
- ]
26
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- return res.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
 
31
  def extract_json(text):
@@ -221,11 +250,27 @@ def generate_document(api_key, raw_input):
221
  raw_input
222
  )
223
 
 
 
 
 
 
 
 
 
224
  designer_json = run_designer(
225
  api_key,
226
  thinker_json
227
  )
228
 
 
 
 
 
 
 
 
 
229
  doc_path = build_word(
230
  designer_json
231
  )
 
10
 
11
  def call_model(client, model_name, system_prompt, user_prompt):
12
 
13
+ try:
14
+
15
+ res = client.chat.completions.create(
16
+ model=model_name,
17
+ temperature=0.2,
18
+ messages=[
19
+ {
20
+ "role": "system",
21
+ "content": system_prompt
22
+ },
23
+ {
24
+ "role": "user",
25
+ "content": user_prompt
26
+ }
27
+ ]
28
+ )
29
+
30
+ # Defensive checks
31
+ if res is None:
32
+
33
+ return "ERROR: Response is None"
34
+
35
+ if not hasattr(res, "choices"):
36
+
37
+ return "ERROR: Response has no choices"
38
+
39
+ if not res.choices:
40
+
41
+ return "ERROR: choices empty"
42
+
43
+ if res.choices[0].message is None:
44
 
45
+ return "ERROR: message empty"
46
+
47
+ content = res.choices[0].message.content
48
+
49
+ if content is None:
50
+
51
+ return "ERROR: content empty"
52
+
53
+ return content
54
+
55
+ except Exception as e:
56
+
57
+ return f"ERROR: {str(e)}"
58
 
59
 
60
  def extract_json(text):
 
250
  raw_input
251
  )
252
 
253
+ if "ERROR:" in thinker_json:
254
+
255
+ return (
256
+ thinker_json,
257
+ "Designer skipped",
258
+ None
259
+ )
260
+
261
  designer_json = run_designer(
262
  api_key,
263
  thinker_json
264
  )
265
 
266
+ if "ERROR:" in designer_json:
267
+
268
+ return (
269
+ thinker_json,
270
+ designer_json,
271
+ None
272
+ )
273
+
274
  doc_path = build_word(
275
  designer_json
276
  )