Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
from typing import Optional
|
4 |
+
from smolagents import (
|
5 |
+
CodeAgent,
|
6 |
+
ToolCallingAgent,
|
7 |
+
DuckDuckGoSearchTool,
|
8 |
+
VisitWebpageTool,
|
9 |
+
HfApiModel
|
10 |
+
)
|
11 |
+
|
12 |
+
|
13 |
+
system_prompt = """
|
14 |
+
You are a specialist tasked with finding films and TV shows using a web search engine, similar to an IMDB expert.
|
15 |
+
Your job is to determine the best match based on the user’s provided description, which may also include cast details and/or the year of release.
|
16 |
+
You have access to several search teams:
|
17 |
+
- One team searches by description only.
|
18 |
+
- A second team searches using description plus approximate cast details.
|
19 |
+
- A third team uses description with approximate year of release.
|
20 |
+
- A fourth team combines description, cast, and year of release for a comprehensive search.
|
21 |
+
|
22 |
+
Follow these steps:
|
23 |
+
1. Identify which details the user has provided (description, cast, release year).
|
24 |
+
2. Direct the query to the corresponding search team to obtain the best results.
|
25 |
+
3. If only a description is given, focus solely on that; if additional details are available, refine the search accordingly.
|
26 |
+
4. Retrieve the film or TV show title that best matches the query, or a list of the most relevant titles if multiple strong candidates exist.
|
27 |
+
5. Finally, output only the title(s) of the film or TV show as your answer.
|
28 |
+
"""
|
29 |
+
|
30 |
+
def agent_init(
|
31 |
+
together_api_token: str,
|
32 |
+
description: str,
|
33 |
+
content_type: Optional[str] = None,
|
34 |
+
additional_info: Optional[str] = None,
|
35 |
+
cast_or_year_of_release: Optional[str] = None
|
36 |
+
):
|
37 |
+
model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", provider="together", max_tokens=4096, token=together_api_token)
|
38 |
+
|
39 |
+
agent_vox114 = ToolCallingAgent(
|
40 |
+
model=model,
|
41 |
+
tools=[VisitWebpageTool(), DuckDuckGoSearchTool()],
|
42 |
+
max_steps=10,
|
43 |
+
planning_interval=4,
|
44 |
+
name="film_seracher",
|
45 |
+
description="""
|
46 |
+
This agent specializes in finding films or TV shows based on a provided description, which may include additional details like cast names and/or the year of release.
|
47 |
+
If the user supplies extra context (e.g., specific actors or release year), the agent will refine the search accordingly.
|
48 |
+
In cases where an exact match isn’t found, it will return a list of the most relevant results.
|
49 |
+
Ensure that the user’s query is a full sentence with clear details to maximize search accuracy.
|
50 |
+
"""
|
51 |
+
)
|
52 |
+
|
53 |
+
agent_vox114.prompt_templates['managed_agent']['task'] += system_prompt
|
54 |
+
|
55 |
+
main_agent = CodeAgent(
|
56 |
+
model=model,
|
57 |
+
tools=[],
|
58 |
+
additional_authorized_imports=["re", "datetime", "json"],
|
59 |
+
managed_agents=[agent_vox114],
|
60 |
+
max_steps=10,
|
61 |
+
planning_interval=4,
|
62 |
+
)
|
63 |
+
|
64 |
+
if content_type is None:
|
65 |
+
content_type = "film or TV show"
|
66 |
+
|
67 |
+
if additional_info is None:
|
68 |
+
additional_info = ""
|
69 |
+
|
70 |
+
if additional_info is None and cast_or_year_of_release is None:
|
71 |
+
cast_or_year_of_release = ""
|
72 |
+
|
73 |
+
if (additional_info and cast_or_year_of_release is None) or (additional_info is None and cast_or_year_of_release):
|
74 |
+
st.error("additional_info or cast_or_year_of_release is None, you cannot use one of them they are work in pair.")
|
75 |
+
return None # Return None to indicate an error
|
76 |
+
|
77 |
+
vox114 = main_agent.run(f"Find the {content_type} by description {additional_info}: {description}. {cast_or_year_of_release}")
|
78 |
+
|
79 |
+
return vox114
|
80 |
+
|
81 |
+
|
82 |
+
def main():
|
83 |
+
st.set_page_config(
|
84 |
+
page_title="Film Finder AI Agent",
|
85 |
+
page_icon="🎬",
|
86 |
+
layout="wide",
|
87 |
+
initial_sidebar_state="expanded",
|
88 |
+
)
|
89 |
+
|
90 |
+
st.title("🎬 Film & TV Show Finder AI Agent")
|
91 |
+
st.markdown("Enter your API token and film/show details below to find what you're looking for!")
|
92 |
+
|
93 |
+
# Sidebar for API Token
|
94 |
+
with st.sidebar:
|
95 |
+
st.header("API Token")
|
96 |
+
together_api_token = st.text_input("Together API Token", type="password", placeholder="Enter your API token here")
|
97 |
+
st.markdown("Get your API token from [Together AI](https://together.ai/)", unsafe_allow_html=True)
|
98 |
+
|
99 |
+
# Main content area
|
100 |
+
with st.container():
|
101 |
+
col1, col2 = st.columns([3, 2]) # Adjust column ratio for better layout
|
102 |
+
|
103 |
+
with col1:
|
104 |
+
st.header("Search Criteria")
|
105 |
+
description = st.text_area("Film/Show Description", placeholder="e.g., A group of wizards fighting a dark lord...", height=150)
|
106 |
+
content_type = st.selectbox("Content Type", ["film", "TV Show"], index=0) # Film is default
|
107 |
+
additional_info_expander = st.expander("Optional Details: Cast & Year of Release", expanded=False)
|
108 |
+
with additional_info_expander:
|
109 |
+
additional_info = st.text_input("Additional Information (e.g., plot points, genre)", placeholder="e.g., set in space, sci-fi")
|
110 |
+
cast_or_year_of_release = st.text_input("Cast or Year of Release (e.g., starring Tom Hanks, released in 2010)", placeholder="e.g., starring Leonardo DiCaprio")
|
111 |
+
|
112 |
+
search_button = st.button("🔍 Search for Film/Show", use_container_width=True)
|
113 |
+
|
114 |
+
with col2:
|
115 |
+
st.header("Search Results")
|
116 |
+
if search_button:
|
117 |
+
if not together_api_token:
|
118 |
+
st.error("Please enter your Together API token in the sidebar.")
|
119 |
+
elif not description:
|
120 |
+
st.warning("Please provide a description of the film or TV show you are looking for.")
|
121 |
+
else:
|
122 |
+
with st.spinner("Agent is searching for the best match..."):
|
123 |
+
result = agent_init(
|
124 |
+
together_api_token=together_api_token,
|
125 |
+
description=description,
|
126 |
+
content_type=content_type.lower(),
|
127 |
+
additional_info=additional_info if additional_info_expander.expanded else None, # Only pass if expander is opened
|
128 |
+
cast_or_year_of_release=cast_or_year_of_release if additional_info_expander.expanded else None # Only pass if expander is opened
|
129 |
+
)
|
130 |
+
|
131 |
+
if result:
|
132 |
+
st.success("Search Results Found!")
|
133 |
+
st.write(result)
|
134 |
+
else:
|
135 |
+
st.warning("No results found or an error occurred. Please check your input and API token.")
|
136 |
+
else:
|
137 |
+
st.info("Enter your search criteria and click 'Search for Film/Show' to begin.")
|
138 |
+
|
139 |
+
# Footer with Agent Details (optional, for extra polish)
|
140 |
+
st.markdown("---")
|
141 |
+
st.caption("Powered by Smolagents by Hugging Face")
|
142 |
+
# st.caption("System Prompt (for reference):")
|
143 |
+
# st.code(system_prompt, language='python') # Show system prompt in a code block for transparency
|
144 |
+
|
145 |
+
|
146 |
+
if __name__ == "__main__":
|
147 |
+
main()
|