Spaces:
Sleeping
Sleeping
Download libs/format.py from AshwinUnnikrishnan/CineMatch2.0: direct link, hf CLI and curl.
- Browser
- Download file 2.47 kB
-
https://huggingface.co/spaces/AshwinUnnikrishnan/CineMatch2.0/resolve/main/libs/format.py
- Command line
-
hf download hf://spaces/AshwinUnnikrishnan/CineMatch2.0/libs/format.py
-
curl -L -o format.py https://huggingface.co/spaces/AshwinUnnikrishnan/CineMatch2.0/resolve/main/libs/format.py
2.47 kB
| """ | |
| This contains functions to format strings to generate query and to convert response to proper format. | |
| """ | |
| import re | |
| from libs.movieClass import Movie | |
| from libs.util import remove_special_characters, replace_special_with_comma | |
| from config.config import REGEX_PATTERN | |
| def get_prompt(languages, details, max_duration, count): | |
| """ | |
| Formats the input parameters into a string that can be used as a prompt for the movie search. | |
| :param languages: The languages across which the user wants to search. | |
| :param details: The details or description of the movies the user is interested in. | |
| :param max_duration: The maximum duration of the movies in hours. | |
| :param count: The maximum number of movies to return. | |
| :return: The formatted prompt string. | |
| """ | |
| if not languages: | |
| languages = "any language" | |
| movie_string = "search for movies" | |
| if "movie" not in details: | |
| details += " " + movie_string | |
| prompt = f"{details} in {languages} with maximum duration of {max_duration} hours and return {count} movies." | |
| return prompt | |
| def process_response(result): | |
| """ | |
| Processes the raw response from the movie search API and converts it into a list of Movie objects. | |
| :param result: The raw response from the movie search API. | |
| :return: A list of Movie objects representing the movies in the response. | |
| """ | |
| # TODO: Randomize the candidate selection | |
| if result.candidates[0].finish_reason == 5: # no response | |
| return [] | |
| response = result.candidates[0].content.parts[0].text | |
| response = response.replace('\n', ' ') | |
| response = response.replace('*', '') | |
| movies = re.findall(REGEX_PATTERN, response) | |
| if len(movies) == 0: | |
| return [] | |
| movies_list = [] | |
| image_search_quota_reached = False | |
| for match in movies: | |
| movie_obj = Movie(remove_special_characters(match[0].strip()), | |
| remove_special_characters(match[1].strip()), | |
| replace_special_with_comma(remove_special_characters(match[2].strip())), | |
| replace_special_with_comma(remove_special_characters(match[3].strip())), | |
| replace_special_with_comma(remove_special_characters(match[4].strip())), | |
| match[5].strip(), image_search_quota_reached) | |
| if not movie_obj.img_url: | |
| image_search_quota_reached = True | |
| movies_list.append(movie_obj) | |
| return movies_list | |