cicboy commited on
Commit
06cf04c
·
1 Parent(s): c7bff07

update app.py file

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -58,15 +58,15 @@ class OMDbConnection:
58
 
59
  try:
60
  data = await self._get(params)
61
- except Exception as e:
62
- return f"ERROR: OMDb request failed: {type(e).__name__}: {e}"
63
 
64
  # 2) If not found, fallback to search then fetch by imdbID
65
  if data.get("Response") != "True":
66
  try:
67
  search = await self._get({"apikey": self.api_key, "s": clean_title, "type": "movie"})
68
- except Exception as e:
69
- return f"ERROR: OMDb search failed: {type(e).__name__}: {e}"
70
 
71
  results = (search or {}).get("Search") or []
72
  if not results:
@@ -83,8 +83,8 @@ class OMDbConnection:
83
 
84
  try:
85
  data = await self._get({"apikey": self.api_key, "i": imdb_id, "plot": "short"})
86
- except Exception as e:
87
- return f"ERROR: OMDb fetch-by-ID failed: {type(e).__name__}: {e}"
88
 
89
  if data.get("Response") != "True":
90
  return f"ERROR: OMDb fetch-by-ID returned no result for imdbID={imdb_id}"
@@ -120,19 +120,22 @@ async def movie_system_prompt(ctx: RunContext[MovieData]) -> str:
120
  return await ctx.deps.omdb_conn.get_movie_info(ctx.deps.title)
121
 
122
  async def run_agent(user_query: str, movie_title: str):
123
- deps = MovieData(title=movie_title, omdb_conn=OMDbConnection(OMDB_API_KEY))
124
-
125
- # Preflight so we return clean UI errors (and avoid LLM call on missing data)
126
- preflight = await deps.omdb_conn.get_movie_info(movie_title)
127
- if preflight.startswith("ERROR:"):
128
- return {"Error": preflight.replace("ERROR:", "").strip()}
129
-
130
- result = await agent.run(user_query, deps=deps)
131
- return {
132
- "Why": result.output.why,
133
- "Rating": int(result.output.rating),
134
- "Recommend": bool(result.output.recommended),
135
- }
 
 
 
136
 
137
  # ---- Gradio UI ----
138
  with gr.Blocks() as demo:
 
58
 
59
  try:
60
  data = await self._get(params)
61
+ except Exception:
62
+ return "ERROR: OMDb request failed. Please try again."
63
 
64
  # 2) If not found, fallback to search then fetch by imdbID
65
  if data.get("Response") != "True":
66
  try:
67
  search = await self._get({"apikey": self.api_key, "s": clean_title, "type": "movie"})
68
+ except Exception:
69
+ return "ERROR: OMDb request failed. Please try again."
70
 
71
  results = (search or {}).get("Search") or []
72
  if not results:
 
83
 
84
  try:
85
  data = await self._get({"apikey": self.api_key, "i": imdb_id, "plot": "short"})
86
+ except Exception:
87
+ return "ERROR: OMDb fetch-by-ID failed. Please try again"
88
 
89
  if data.get("Response") != "True":
90
  return f"ERROR: OMDb fetch-by-ID returned no result for imdbID={imdb_id}"
 
120
  return await ctx.deps.omdb_conn.get_movie_info(ctx.deps.title)
121
 
122
  async def run_agent(user_query: str, movie_title: str):
123
+ try:
124
+ deps = MovieData(title=movie_title, omdb_conn=OMDbConnection(OMDB_API_KEY))
125
+
126
+ # Preflight so we return clean UI errors (and avoid LLM call on missing data)
127
+ preflight = await deps.omdb_conn.get_movie_info(movie_title)
128
+ if preflight.startswith("ERROR:"):
129
+ return {"Error": preflight.replace("ERROR:", "").strip()}
130
+
131
+ result = await agent.run(user_query, deps=deps)
132
+ return {
133
+ "Why": result.output.why,
134
+ "Rating": int(result.output.rating),
135
+ "Recommend": bool(result.output.recommended),
136
+ }
137
+ except Exception:
138
+ return {"Error": "Something went wrong. Please try again."}
139
 
140
  # ---- Gradio UI ----
141
  with gr.Blocks() as demo: