Prof-Reza commited on
Commit
9098a8c
·
verified ·
1 Parent(s): 33f1631

Handle missing API keys in app.py: add try/except around OpenAI, search, planner, and generator calls to display helpful error messages.

Browse files
Files changed (1) hide show
  1. app.py +46 -11
app.py CHANGED
@@ -28,13 +28,21 @@ def chat(user_message, chat_history, chat_pairs, sources, plan):
28
  # build messages including system prompt
29
  messages = [{"role": "system", "content": SYSTEM_PROMPT}] + chat_history
30
  # call OpenAI's ChatCompletion to get assistant's reply
31
- response = openai.ChatCompletion.create(
32
- model=os.getenv("OPENAI_MODEL", "gpt-5-mini"),
33
- messages=messages,
34
- temperature=float(os.getenv("TEMPERATURE", "0.7")),
35
- max_tokens=int(os.getenv("MAX_OUTPUT_TOKENS", "1024")),
36
- )
37
- assistant_reply = response["choices"][0]["message"]["content"]
 
 
 
 
 
 
 
 
38
  # append assistant reply to conversation history
39
  chat_history.append({"role": "assistant", "content": assistant_reply})
40
  # append pair to display history for Chatbot component
@@ -44,7 +52,16 @@ def chat(user_message, chat_history, chat_pairs, sources, plan):
44
  def run_search(query, chat_history, chat_pairs, sources, plan, num_results=5, domain_filter=""):
45
  """Execute a web search and update sources list."""
46
  # perform search using provided searcher
47
- results = run_web_search(query, num_results=num_results, domain_filter=domain_filter)
 
 
 
 
 
 
 
 
 
48
  if sources is None:
49
  sources = []
50
  sources.extend(results)
@@ -64,7 +81,13 @@ def finalize_outline(chat_history, chat_pairs, sources, plan):
64
  if sources is None:
65
  sources = []
66
  # use the planner to create the plan
67
- plan_text = plan_course(chat_history, sources)
 
 
 
 
 
 
68
  plan = plan_text
69
  return plan_text, chat_history, chat_pairs, sources, plan
70
 
@@ -75,8 +98,20 @@ def generate_package(plan, sources):
75
  plan = "Course plan is empty."
76
  if sources is None:
77
  sources = []
78
- zip_path = generate_course_zip(plan, sources)
79
- return zip_path
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  with gr.Blocks() as demo:
82
  gr.Markdown(
 
28
  # build messages including system prompt
29
  messages = [{"role": "system", "content": SYSTEM_PROMPT}] + chat_history
30
  # call OpenAI's ChatCompletion to get assistant's reply
31
+ try:
32
+ response = openai.ChatCompletion.create(
33
+ model=os.getenv("OPENAI_MODEL", "gpt-5-mini"),
34
+ messages=messages,
35
+ temperature=float(os.getenv("TEMPERATURE", "0.7")),
36
+ max_tokens=int(os.getenv("MAX_OUTPUT_TOKENS", "1024")),
37
+ )
38
+ assistant_reply = response["choices"][0]["message"]["content"]
39
+ except Exception as e:
40
+ # When the API call fails (e.g. missing API key), return an error message
41
+ assistant_reply = (
42
+ "An error occurred while processing your message. "
43
+ "Please ensure your OpenAI API key is configured in the Space secrets.\n"
44
+ f"(Error: {e})"
45
+ )
46
  # append assistant reply to conversation history
47
  chat_history.append({"role": "assistant", "content": assistant_reply})
48
  # append pair to display history for Chatbot component
 
52
  def run_search(query, chat_history, chat_pairs, sources, plan, num_results=5, domain_filter=""):
53
  """Execute a web search and update sources list."""
54
  # perform search using provided searcher
55
+ try:
56
+ results = run_web_search(query, num_results=num_results, domain_filter=domain_filter)
57
+ except Exception as e:
58
+ # handle search errors (e.g. missing API key)
59
+ results = []
60
+ summary = (
61
+ "An error occurred during web search. Please ensure your search API key is configured.\n"
62
+ f"(Error: {e})"
63
+ )
64
+ return summary, chat_history, chat_pairs, sources or [], plan
65
  if sources is None:
66
  sources = []
67
  sources.extend(results)
 
81
  if sources is None:
82
  sources = []
83
  # use the planner to create the plan
84
+ try:
85
+ plan_text = plan_course(chat_history, sources)
86
+ except Exception as e:
87
+ plan_text = (
88
+ "An error occurred while generating the course outline. Please ensure your API keys are configured.\n"
89
+ f"(Error: {e})"
90
+ )
91
  plan = plan_text
92
  return plan_text, chat_history, chat_pairs, sources, plan
93
 
 
98
  plan = "Course plan is empty."
99
  if sources is None:
100
  sources = []
101
+ try:
102
+ zip_path = generate_course_zip(plan, sources)
103
+ return zip_path
104
+ except Exception as e:
105
+ # On error, return a message as a text file inside an in-memory file path to avoid raising exceptions in Gradio
106
+ err_msg = (
107
+ "An error occurred while generating the course package. Please check your API keys or input.\n"
108
+ f"(Error: {e})"
109
+ )
110
+ # Create a text file to return with the error message
111
+ tmp_path = "/tmp/error.txt"
112
+ with open(tmp_path, "w") as f:
113
+ f.write(err_msg)
114
+ return tmp_path
115
 
116
  with gr.Blocks() as demo:
117
  gr.Markdown(