Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,6 +30,7 @@ st.markdown("""
|
|
| 30 |
border-radius: 15px;
|
| 31 |
text-align: center;
|
| 32 |
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
|
|
|
| 33 |
}
|
| 34 |
.response-box {
|
| 35 |
background: rgba(255,255,255,0.1);
|
|
@@ -49,19 +50,47 @@ st.markdown("""
|
|
| 49 |
.stButton>button:hover {
|
| 50 |
transform: scale(1.05);
|
| 51 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
</style>
|
| 53 |
""", unsafe_allow_html=True)
|
| 54 |
|
| 55 |
# --------------------------
|
| 56 |
-
#
|
| 57 |
# --------------------------
|
| 58 |
-
# Replace load_movie_data() with:
|
| 59 |
@st.cache_resource
|
| 60 |
def load_movie_data():
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
@st.cache_resource
|
| 67 |
def setup_retrieval(df):
|
|
@@ -73,38 +102,40 @@ def setup_retrieval(df):
|
|
| 73 |
return embedder, index
|
| 74 |
|
| 75 |
# --------------------------
|
| 76 |
-
# Groq API
|
| 77 |
# --------------------------
|
| 78 |
-
def
|
| 79 |
-
|
| 80 |
-
api_key=os.getenv("GROQ_API_KEY", "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")
|
| 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 |
# Main Application
|
| 106 |
# --------------------------
|
| 107 |
def main():
|
|
|
|
| 108 |
df = load_movie_data()
|
| 109 |
embedder, index = setup_retrieval(df)
|
| 110 |
|
|
@@ -122,40 +153,44 @@ def main():
|
|
| 122 |
st.subheader("Sample Questions")
|
| 123 |
examples = [
|
| 124 |
"Who played the Joker in The Dark Knight?",
|
| 125 |
-
"
|
| 126 |
-
"List
|
| 127 |
-
"
|
| 128 |
-
"
|
| 129 |
]
|
| 130 |
for ex in examples:
|
| 131 |
st.code(ex, language="bash")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
# Main Interface
|
| 134 |
query = st.text_input("π― Ask any movie question:",
|
| 135 |
placeholder="e.g., 'Who played the villain in The Dark Knight?'")
|
| 136 |
|
| 137 |
-
if st.button("π Get
|
| 138 |
if query:
|
| 139 |
-
with st.spinner("π Searching through
|
| 140 |
query_embed = embedder.encode([query])
|
| 141 |
-
_, indices = index.search(query_embed,
|
| 142 |
contexts = [df.iloc[i]['context'] for i in indices[0]]
|
| 143 |
-
combined_context = "\n\n".join(contexts)
|
| 144 |
|
| 145 |
with st.spinner("π₯ Generating cinematic insights..."):
|
| 146 |
-
answer =
|
| 147 |
|
| 148 |
st.markdown("---")
|
| 149 |
with st.container():
|
| 150 |
st.markdown("## π¬ Expert Analysis")
|
| 151 |
st.markdown(f'<div class="response-box">{answer}</div>', unsafe_allow_html=True)
|
| 152 |
|
| 153 |
-
st.markdown("## π
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
with st.expander(f"Source {i+1}", expanded=True):
|
| 158 |
-
st.write(ctx)
|
| 159 |
else:
|
| 160 |
st.warning("Please enter a movie-related question")
|
| 161 |
|
|
|
|
| 30 |
border-radius: 15px;
|
| 31 |
text-align: center;
|
| 32 |
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 33 |
+
margin-bottom: 2rem;
|
| 34 |
}
|
| 35 |
.response-box {
|
| 36 |
background: rgba(255,255,255,0.1);
|
|
|
|
| 50 |
.stButton>button:hover {
|
| 51 |
transform: scale(1.05);
|
| 52 |
}
|
| 53 |
+
.movie-card {
|
| 54 |
+
background: rgba(0,0,0,0.2);
|
| 55 |
+
border-radius: 10px;
|
| 56 |
+
padding: 1rem;
|
| 57 |
+
margin: 0.5rem 0;
|
| 58 |
+
}
|
| 59 |
</style>
|
| 60 |
""", unsafe_allow_html=True)
|
| 61 |
|
| 62 |
# --------------------------
|
| 63 |
+
# Data Loading & Processing
|
| 64 |
# --------------------------
|
|
|
|
| 65 |
@st.cache_resource
|
| 66 |
def load_movie_data():
|
| 67 |
+
try:
|
| 68 |
+
# Try loading wiki_movies dataset
|
| 69 |
+
dataset = load_dataset("wikipedia", "20220301.en", split="train[:5000]")
|
| 70 |
+
df = pd.DataFrame(dataset)
|
| 71 |
+
|
| 72 |
+
# Create synthetic movie data from Wikipedia snippets
|
| 73 |
+
df['title'] = df['title'].apply(lambda x: x.replace("_", " "))
|
| 74 |
+
df['context'] = "Title: " + df['title'] + "\nContent: " + df['text'].str[:500] + "..."
|
| 75 |
+
return df.sample(1000) # Return random 1000 entries
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
st.warning(f"Couldn't load dataset: {str(e)}. Using synthetic data.")
|
| 79 |
+
movies = [
|
| 80 |
+
{
|
| 81 |
+
"title": "The Dark Knight",
|
| 82 |
+
"context": "Title: The Dark Knight\nPlot: Batman faces the Joker in a battle for Gotham's soul...\nCast: Christian Bale, Heath Ledger\nYear: 2008\nDirector: Christopher Nolan"
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"title": "Inception",
|
| 86 |
+
"context": "Title: Inception\nPlot: A thief who enters the dreams of others...\nCast: Leonardo DiCaprio, Tom Hardy\nYear: 2010\nDirector: Christopher Nolan"
|
| 87 |
+
},
|
| 88 |
+
{
|
| 89 |
+
"title": "Pulp Fiction",
|
| 90 |
+
"context": "Title: Pulp Fiction\nPlot: The lives of two mob hitmen, a boxer, and a gangster's wife intertwine...\nCast: John Travolta, Samuel L. Jackson\nYear: 1994\nDirector: Quentin Tarantino"
|
| 91 |
+
}
|
| 92 |
+
]
|
| 93 |
+
return pd.DataFrame(movies)
|
| 94 |
|
| 95 |
@st.cache_resource
|
| 96 |
def setup_retrieval(df):
|
|
|
|
| 102 |
return embedder, index
|
| 103 |
|
| 104 |
# --------------------------
|
| 105 |
+
# Groq API Functions
|
| 106 |
# --------------------------
|
| 107 |
+
def get_groq_response(query, context):
|
| 108 |
+
try:
|
| 109 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY", "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"))
|
| 110 |
+
|
| 111 |
+
prompt = f"""You are a film expert analyzing this question:
|
| 112 |
+
|
| 113 |
+
Question: {query}
|
| 114 |
+
|
| 115 |
+
Using these verified sources:
|
| 116 |
+
{context}
|
| 117 |
+
|
| 118 |
+
Provide a detailed response with:
|
| 119 |
+
1. π¬ Direct Answer
|
| 120 |
+
2. π Explanation
|
| 121 |
+
3. π₯ Relevant Scenes
|
| 122 |
+
4. π Awards/Trivia (if available)
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
response = client.chat.completions.create(
|
| 126 |
+
messages=[{"role": "user", "content": prompt}],
|
| 127 |
+
model="llama3-70b-8192",
|
| 128 |
+
temperature=0.3
|
| 129 |
+
)
|
| 130 |
+
return response.choices[0].message.content
|
| 131 |
+
except Exception as e:
|
| 132 |
+
return f"Error getting response: {str(e)}"
|
| 133 |
|
| 134 |
# --------------------------
|
| 135 |
# Main Application
|
| 136 |
# --------------------------
|
| 137 |
def main():
|
| 138 |
+
# Load data and models
|
| 139 |
df = load_movie_data()
|
| 140 |
embedder, index = setup_retrieval(df)
|
| 141 |
|
|
|
|
| 153 |
st.subheader("Sample Questions")
|
| 154 |
examples = [
|
| 155 |
"Who played the Joker in The Dark Knight?",
|
| 156 |
+
"Explain the ending of Inception",
|
| 157 |
+
"List Tarantino's movies",
|
| 158 |
+
"What's the plot of Pulp Fiction?",
|
| 159 |
+
"Who directed The Dark Knight?"
|
| 160 |
]
|
| 161 |
for ex in examples:
|
| 162 |
st.code(ex, language="bash")
|
| 163 |
+
|
| 164 |
+
st.markdown("---")
|
| 165 |
+
st.markdown("**Database Info**")
|
| 166 |
+
st.write(f"π {len(df)} movies loaded")
|
| 167 |
+
st.write("π Using FAISS for vector search")
|
| 168 |
+
st.write("π€ Powered by Llama 3 70B")
|
| 169 |
|
| 170 |
# Main Interface
|
| 171 |
query = st.text_input("π― Ask any movie question:",
|
| 172 |
placeholder="e.g., 'Who played the villain in The Dark Knight?'")
|
| 173 |
|
| 174 |
+
if st.button("π Get Expert Analysis", type="primary"):
|
| 175 |
if query:
|
| 176 |
+
with st.spinner("π Searching through movie database..."):
|
| 177 |
query_embed = embedder.encode([query])
|
| 178 |
+
_, indices = index.search(query_embed, 3)
|
| 179 |
contexts = [df.iloc[i]['context'] for i in indices[0]]
|
| 180 |
+
combined_context = "\n\n---\n\n".join(contexts)
|
| 181 |
|
| 182 |
with st.spinner("π₯ Generating cinematic insights..."):
|
| 183 |
+
answer = get_groq_response(query, combined_context)
|
| 184 |
|
| 185 |
st.markdown("---")
|
| 186 |
with st.container():
|
| 187 |
st.markdown("## π¬ Expert Analysis")
|
| 188 |
st.markdown(f'<div class="response-box">{answer}</div>', unsafe_allow_html=True)
|
| 189 |
|
| 190 |
+
st.markdown("## π Reference Materials")
|
| 191 |
+
for i, ctx in enumerate(contexts, 1):
|
| 192 |
+
with st.expander(f"Source {i}", expanded=(i==1)):
|
| 193 |
+
st.markdown(f'<div class="movie-card">{ctx}</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
| 194 |
else:
|
| 195 |
st.warning("Please enter a movie-related question")
|
| 196 |
|