-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
375 lines (320 loc) · 14.5 KB
/
app.py
File metadata and controls
375 lines (320 loc) · 14.5 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import json
import os
from base64 import b64decode, b64encode
from pathlib import Path
from typing import List, Optional, Tuple
import numpy as np
import requests
import streamlit as st
st.set_page_config(page_title="Gemini Query Fanout", layout="centered")
# ---------------------------- Utils ----------------------------
def custom_cosine_similarity(a: List[float], b: List[float]) -> float:
try:
a = np.asarray(a, dtype=np.float64)
b = np.asarray(b, dtype=np.float64)
dot_product = np.dot(a, b)
norm_a = np.linalg.norm(a)
norm_b = np.linalg.norm(b)
if norm_a == 0 or norm_b == 0:
return 0.0
similarity = dot_product / (norm_a * norm_b)
return float(np.clip(similarity, -1.0, 1.0))
except Exception:
return 0.0
def calculate_cosine_similarities(
query_embedding: List[float], question_embeddings: List[List[float]]
) -> Optional[np.ndarray]:
try:
similarities = [
custom_cosine_similarity(query_embedding, q) for q in question_embeddings
]
arr = np.array(similarities)
return arr
except Exception:
return None
# ---------------------------- Gemini APIs ----------------------------
@st.cache_data(show_spinner=False, ttl=300)
def get_available_models(api_key: str) -> List[str]:
"""Fetch available models from Gemini API that support generateContent.
Cached for 5 minutes to avoid excessive API calls.
"""
url = f"https://generativelanguage.googleapis.com/v1beta/models?key={api_key}"
try:
response = requests.get(url, timeout=30)
response.raise_for_status()
result = response.json()
models = []
if "models" in result:
for model in result["models"]:
# Only include models that support generateContent
if "generateContent" in model.get("supportedGenerationMethods", []):
# Extract model name (e.g., "models/gemini-1.5-flash" -> "gemini-1.5-flash")
model_name = model.get("name", "").replace("models/", "")
if model_name:
models.append(model_name)
# Sort models with preview/experimental first, then by version
models.sort(reverse=True)
return models if models else ["gemini-3-flash-preview"] # Fallback
except Exception as e:
st.warning(f"Could not fetch models from API: {e}. Using default list.")
# Fallback to a default list
return [
"gemini-3-flash-preview",
"gemini-2.0-flash-exp",
"gemini-1.5-flash",
"gemini-1.5-flash-002",
"gemini-1.5-flash-8b",
"gemini-1.5-pro",
"gemini-1.5-pro-002",
]
@st.cache_data(show_spinner=False)
def get_gemini_embeddings(
texts: Tuple[str, ...], api_key: str
) -> Optional[List[List[float]]]:
"""Get embeddings from Google Gemini embedding API for a tuple of texts.
Uses caching to avoid repeated calls for the same inputs.
"""
embeddings: List[List[float]] = []
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent?key={api_key}"
headers = {"Content-Type": "application/json"}
for i, text in enumerate(texts):
data = {"model": "gemini-embedding-001", "content": {"parts": [{"text": text}]}}
try:
resp = requests.post(url, headers=headers, json=data, timeout=60)
resp.raise_for_status()
result = resp.json()
if "embedding" in result and "values" in result["embedding"]:
embeddings.append(result["embedding"]["values"])
else:
st.error(f"Invalid embedding response structure for text #{i+1}")
return None
except Exception as e:
st.error(f"Error getting embedding for text #{i+1}: {e}")
return None
if len(embeddings) == len(texts):
return embeddings
else:
return None
@st.cache_data(show_spinner=False)
def query_fanout_and_rank(
keyword: str, language: str, top_n: int, api_key: str, model: str, temperature: float, top_k: int, top_p: float, max_output_tokens: int
) -> Optional[Tuple[List[str], List[float], Optional[str]]]:
"""Generate 10 questions using Gemini generate API and rank them by cosine similarity.
Returns top_n questions plus their similarity scores and the raw text response (when available).
"""
# Build system and user prompts (kept similar to notebook)
system_prompt = (
'You are an advanced AI search assistant. Your task is to use the "query fan-out" technique '
"to anticipate a user's complete informational need from a single keyword.\n\n"
"You will generate a list of 10 highly semantically related and comprehensive short questions that a user might have based on this keyword.\n\n"
'After completing your analysis, provide your output in a structured JSON format. The JSON should contain an array named "questions" with 10 string elements. The final output should contain only the JSON structure.'
)
user_prompt = f"The keyword you will analyze is:\n<keyword>\n{keyword}\n</keyword>\n\nLanguage of keyword, operation and output language:\n<language>\n{language}\n</language>"
payload = {
"contents": [{"parts": [{"text": system_prompt}, {"text": user_prompt}]}],
"generationConfig": {
"temperature": temperature,
"topK": top_k,
"topP": top_p,
"maxOutputTokens": max_output_tokens,
},
}
gen_url = f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={api_key}"
headers = {"Content-Type": "application/json"}
raw_text: Optional[str] = None
try:
response = requests.post(gen_url, headers=headers, json=payload, timeout=60)
response.raise_for_status()
result = response.json()
if "candidates" in result and len(result["candidates"]) > 0:
content = result["candidates"][0].get("content", {})
if "parts" in content and len(content["parts"]) > 0:
text_response = content["parts"][0].get("text", "").strip()
raw_text = text_response
# Trim code fence markers if present
if text_response.startswith("```json"):
text_response = text_response[7:]
if text_response.startswith("```"):
text_response = text_response[3:]
if text_response.endswith("```"):
text_response = text_response[:-3]
text_response = text_response.strip()
try:
questions_data = json.loads(text_response)
if "questions" in questions_data:
questions = questions_data["questions"]
# Get embeddings for keyword + questions
all_texts = tuple([keyword] + questions)
embeddings = get_gemini_embeddings(all_texts, api_key)
if embeddings and len(embeddings) == len(all_texts):
query_embedding = embeddings[0]
question_embeddings = embeddings[1:]
similarities = calculate_cosine_similarities(
query_embedding, question_embeddings
)
if similarities is not None:
sim_results = [
(i, float(similarities[i]), questions[i])
for i in range(len(questions))
]
sim_results.sort(key=lambda x: x[1], reverse=True)
top = sim_results[:top_n]
top_questions = [t[2] for t in top]
top_scores = [t[1] for t in top]
return top_questions, top_scores, raw_text
else:
st.warning(
"Failed to compute similarities, returning unranked top questions"
)
return questions[:top_n], [None] * top_n, raw_text
else:
st.warning(
"Failed to get embeddings, returning unranked top questions"
)
return questions[:top_n], [None] * top_n, raw_text
else:
st.error("Response JSON does not contain 'questions' field.")
return None
except json.JSONDecodeError as e:
st.error(f"Failed parsing JSON from model: {e}")
return None
st.error("Invalid response structure from Gemini generate API")
return None
except requests.exceptions.RequestException as e:
st.error(f"Error calling Gemini API: {e}")
return None
except Exception as e:
st.error(f"Unexpected error: {e}")
return None
# ---------------------------- Streamlit UI ----------------------------
def main():
st.title("🤖 Gemini Query Fanout")
st.markdown(
"Generate 10 semantically related questions from a keyword using Gemini and rank them using embeddings."
)
with st.form(key="fanout_form"):
keyword = st.text_input(
"Keyword", value="", placeholder="Enter a keyword to analyze"
)
language = st.text_input(
"Language", value="Polish", placeholder="e.g., English, Polish"
)
# API Key
api_key = st.text_input(
"Gemini API Key",
type="password",
help="Enter your Gemini API key"
)
# Model selection with refresh capability
st.markdown("---")
col1, col2 = st.columns([4, 1])
with col1:
# Get available models (will use cached result or fetch from API)
if "available_models" not in st.session_state:
st.session_state.available_models = None
# Use cached models or default list
available_models = st.session_state.available_models or [
"gemini-3-flash-preview",
"gemini-2.0-flash-exp",
"gemini-1.5-flash",
"gemini-1.5-flash-002",
"gemini-1.5-flash-8b",
"gemini-1.5-pro",
"gemini-1.5-pro-002",
]
# Ensure gemini-3-flash-preview is first if present
if "gemini-3-flash-preview" in available_models:
available_models.remove("gemini-3-flash-preview")
available_models.insert(0, "gemini-3-flash-preview")
model = st.selectbox(
"Model",
options=available_models,
index=0,
help="Select the Gemini model to use for generation"
)
with col2:
st.markdown("<br>", unsafe_allow_html=True) # Align button with selectbox
refresh_clicked = st.form_submit_button("🔄", help="Refresh model list from API")
top_n = st.slider("Top N results", 1, 10, 5)
# Generation Config Parameters
with st.expander("⚙️ Generation Config", expanded=False):
temperature = st.slider(
"Temperature",
min_value=0.0,
max_value=1.0,
value=0.7,
step=0.05,
help="Controls randomness: lower is more focused, higher is more creative"
)
top_k = st.number_input(
"Top K",
min_value=1,
max_value=100,
value=40,
step=1,
help="Limits sampling to top K tokens"
)
top_p = st.slider(
"Top P",
min_value=0.0,
max_value=1.0,
value=0.95,
step=0.05,
help="Nucleus sampling: cumulative probability threshold"
)
max_output_tokens = st.number_input(
"Max Output Tokens",
min_value=1,
max_value=8196,
value=2048,
step=128,
help="Maximum length of generated response"
)
show_raw = st.checkbox("Show raw model text response")
submit = st.form_submit_button("Generate")
# Handle refresh button click outside form to avoid conflicts
if refresh_clicked:
if api_key:
# Clear cache and fetch fresh models
get_available_models.clear()
st.session_state.available_models = get_available_models(api_key)
st.rerun()
else:
st.warning("⚠️ Enter API key first to refresh models")
if submit:
if not keyword:
st.error("Keyword cannot be empty")
return
if not api_key:
st.error("API Key is required")
return
with st.spinner("Generating questions and computing similarities..."):
result = query_fanout_and_rank(keyword, language, top_n, api_key, model, temperature, top_k, top_p, max_output_tokens)
if result:
questions, scores, raw_text = result
st.success(f"Top {len(questions)} questions generated")
for i, (q, s) in enumerate(zip(questions, scores), start=1):
if s is not None:
st.write(f"**{i}.** [{s:.3f}] {q}")
else:
st.write(f"**{i}.** {q}")
if show_raw and raw_text:
with st.expander("Raw model text"):
st.code(raw_text)
output_json = json.dumps(
{"questions": questions, "similarities": scores, "raw_text": raw_text},
ensure_ascii=False,
indent=2,
)
st.download_button(
"Download JSON",
data=output_json,
file_name=f"query_fanout_{keyword}.json",
mime="application/json",
)
st.markdown("---")
st.markdown(
"**Notes:**\n- The API key is sent to Google's Gemini API endpoints.\n- Use the `Download JSON` button to save results locally."
)
if __name__ == "__main__":
main()