Repository navigation
Expand file tree
/
Copy pathRecommender.py
More file actions
150 lines (125 loc) · 5.57 KB
/
Copy pathRecommender.py
File metadata and controls
150 lines (125 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import json
import requests
from textwrap import dedent
# ANSI escape codes for colors
class Colors:
PURPLE = '\033[95m'
PINK = '\033[94m' # More like a blue, but let's call it pink for theme
CYAN = '\033[96m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def get_anime_suggestions(favorite_anime: str, api_key: str) -> list | None:
"""
Fetches anime suggestions from the Gemini API.
Args:
favorite_anime: The name of the user's favorite anime.
api_key: The Google Gemini API key.
Returns:
A list of suggested anime dictionaries or None if an error occurs.
"""
print(f"\n{Colors.CYAN}Searching for anime similar to '{favorite_anime}'...{Colors.ENDC}")
# The prompt for the AI model
prompt = f"Based on the anime \"{favorite_anime}\", suggest 5 similar anime. For each suggestion, provide a one-sentence synopsis and its primary genre."
# The expected JSON schema for the response
schema = {
"type": "ARRAY",
"items": {
"type": "OBJECT",
"properties": {
"title": { "type": "STRING" },
"synopsis": { "type": "STRING" },
"genre": { "type": "STRING" },
},
"required": ["title", "synopsis", "genre"]
}
}
# Prepare payload for the Gemini API
payload = {
"contents": [{
"role": "user",
"parts": [{ "text": prompt }]
}],
"generationConfig": {
"responseMimeType": "application/json",
"responseSchema": schema,
},
}
api_url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={api_key}"
try:
response = requests.post(api_url, json=payload, headers={'Content-Type': 'application/json'})
response.raise_for_status() # Raises an HTTPError for bad responses (4xx or 5xx)
result = response.json()
# Process the API response
if (result.get("candidates") and
result["candidates"][0].get("content") and
result["candidates"][0]["content"].get("parts")):
suggestions_json_str = result["candidates"][0]["content"]["parts"][0]["text"]
return json.loads(suggestions_json_str)
else:
print(f"{Colors.RED}Error: Received an unexpected response format from the API.{Colors.ENDC}")
print(f"Response: {result}")
return None
except requests.exceptions.HTTPError as http_err:
print(f"{Colors.RED}HTTP error occurred: {http_err}{Colors.ENDC}")
print(f"{Colors.YELLOW}Response Body: {response.text}{Colors.ENDC}")
except requests.exceptions.RequestException as req_err:
print(f"{Colors.RED}A network error occurred: {req_err}{Colors.ENDC}")
except json.JSONDecodeError:
print(f"{Colors.RED}Error: Failed to parse the response from the API.{Colors.ENDC}")
except Exception as e:
print(f"{Colors.RED}An unexpected error occurred: {e}{Colors.ENDC}")
return None
def display_suggestions(suggestions: list):
"""
Displays the anime suggestions in a formatted way.
Args:
suggestions: A list of anime dictionaries to display.
"""
print(f"\n{Colors.BOLD}{Colors.PURPLE}--- Here are your recommendations ---{Colors.ENDC}\n")
for i, anime in enumerate(suggestions, 1):
print(f" {Colors.BOLD}{Colors.PINK}{i}. {anime.get('title', 'N/A')}{Colors.ENDC}")
print(f" {Colors.GREEN}Genre:{Colors.ENDC} {anime.get('genre', 'N/A')}")
print(f" {Colors.YELLOW}Synopsis:{Colors.ENDC} {anime.get('synopsis', 'N/A')}\n")
print(f"{Colors.BOLD}{Colors.PURPLE}------------------------------------{Colors.ENDC}")
def main():
"""
Main function to run the anime recommender CLI application.
"""
# Get API key from environment variable
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
print(f"{Colors.RED}{Colors.BOLD}Error: GEMINI_API_KEY environment variable not found.{Colors.ENDC}")
print(f"{Colors.YELLOW}Please set your Google Gemini API key as an environment variable.{Colors.ENDC}")
print("Example: export GEMINI_API_KEY='your_api_key_here'")
return
# App header
header = dedent(f"""
{Colors.BOLD}{Colors.PURPLE}
======================================
🎬 Welcome to Anime Recommender! 🎬
======================================
{Colors.ENDC}
""")
print(header)
print(f"{Colors.CYAN}Find your next favorite show with the power of AI.{Colors.ENDC}")
print(f"Type 'quit' or 'exit' to close the application.\n")
while True:
favorite_anime = input(f"{Colors.BOLD}Enter your favorite anime: {Colors.ENDC}").strip()
if favorite_anime.lower() in ['quit', 'exit']:
print(f"\n{Colors.PINK}Happy watching! Goodbye!{Colors.ENDC}")
break
if not favorite_anime:
print(f"{Colors.RED}Please enter an anime title.{Colors.ENDC}")
continue
suggestions = get_anime_suggestions(favorite_anime, api_key)
if suggestions:
display_suggestions(suggestions)
else:
print(f"\n{Colors.YELLOW}Could not retrieve suggestions. Please try another anime or check the error messages.{Colors.ENDC}\n")
if __name__ == "__main__":
main()