Spaces:
Sleeping
Sleeping
update app.py file
Browse files
app.py
CHANGED
|
@@ -58,15 +58,15 @@ class OMDbConnection:
|
|
| 58 |
|
| 59 |
try:
|
| 60 |
data = await self._get(params)
|
| 61 |
-
except Exception
|
| 62 |
-
return
|
| 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
|
| 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
|
| 87 |
-
return
|
| 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 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 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:
|