Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -100,15 +100,25 @@ def get_model(temperature, top_p, repetition_penalty):
|
|
100 |
huggingfacehub_api_token=huggingface_token
|
101 |
)
|
102 |
|
103 |
-
def generate_chunked_response(model, prompt, max_tokens=
|
104 |
full_response = ""
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
109 |
full_response += chunk
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
break
|
111 |
-
|
112 |
return full_response.strip()
|
113 |
|
114 |
def manage_conversation_history(question, answer, history, max_history=5):
|
@@ -223,8 +233,14 @@ def google_search(term, num_results=20, lang="en", timeout=5, safe="active", ssl
|
|
223 |
return all_results
|
224 |
|
225 |
def summarize_content(content, model):
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
summary_prompt = f"""
|
227 |
-
Summarize the following content
|
228 |
{content}
|
229 |
Summary:
|
230 |
"""
|
@@ -262,12 +278,28 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
262 |
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
263 |
else:
|
264 |
database = None
|
265 |
-
|
266 |
if web_search:
|
267 |
search_results = google_search(question)
|
268 |
model = get_model(temperature, top_p, repetition_penalty)
|
269 |
|
270 |
-
summaries = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
titles = [result["title"] for result in search_results]
|
272 |
ranks = rank_search_results(titles, summaries, model)
|
273 |
|
|
|
100 |
huggingfacehub_api_token=huggingface_token
|
101 |
)
|
102 |
|
103 |
+
def generate_chunked_response(model, prompt, max_tokens=200):
|
104 |
full_response = ""
|
105 |
+
total_length = len(prompt.split()) # Approximate token count of prompt
|
106 |
+
|
107 |
+
while total_length < 7800: # Leave some margin
|
108 |
+
try:
|
109 |
+
chunk = model(prompt + full_response, max_new_tokens=min(200, 7800 - total_length))
|
110 |
+
chunk = chunk.strip()
|
111 |
+
if not chunk:
|
112 |
+
break
|
113 |
full_response += chunk
|
114 |
+
total_length += len(chunk.split()) # Approximate token count
|
115 |
+
|
116 |
+
if chunk.endswith((".", "!", "?")):
|
117 |
+
break
|
118 |
+
except Exception as e:
|
119 |
+
print(f"Error generating response: {str(e)}")
|
120 |
break
|
121 |
+
|
122 |
return full_response.strip()
|
123 |
|
124 |
def manage_conversation_history(question, answer, history, max_history=5):
|
|
|
233 |
return all_results
|
234 |
|
235 |
def summarize_content(content, model):
|
236 |
+
# Approximate the token limit using character count
|
237 |
+
# Assuming an average of 4 characters per token
|
238 |
+
max_chars = 7000 * 4 # Leave some room for the prompt
|
239 |
+
if len(content) > max_chars:
|
240 |
+
content = content[:max_chars] + "..."
|
241 |
+
|
242 |
summary_prompt = f"""
|
243 |
+
Summarize the following content concisely:
|
244 |
{content}
|
245 |
Summary:
|
246 |
"""
|
|
|
278 |
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
279 |
else:
|
280 |
database = None
|
281 |
+
|
282 |
if web_search:
|
283 |
search_results = google_search(question)
|
284 |
model = get_model(temperature, top_p, repetition_penalty)
|
285 |
|
286 |
+
summaries = []
|
287 |
+
for result in search_results:
|
288 |
+
try:
|
289 |
+
summary = summarize_content(result["text"], model)
|
290 |
+
summaries.append(summary)
|
291 |
+
except Exception as e:
|
292 |
+
print(f"Error summarizing content: {str(e)}")
|
293 |
+
summaries.append("Error: Unable to summarize this content.")
|
294 |
+
|
295 |
+
# Combine summaries, ensuring we don't exceed the token limit
|
296 |
+
combined_summaries = ""
|
297 |
+
for summary in summaries:
|
298 |
+
if len((combined_summaries + summary).split()) > 7000:
|
299 |
+
break
|
300 |
+
combined_summaries += summary + "\n\n"
|
301 |
+
|
302 |
+
context_str = combined_summaries
|
303 |
titles = [result["title"] for result in search_results]
|
304 |
ranks = rank_search_results(titles, summaries, model)
|
305 |
|