Update app.py
Browse files147 unmodified lines14814915015115215315 unmodified lines1691701711721731741751761771781791801811821831841851 unmodified line187188189190191192193194195196197147 unmodified linesall_results = []max_chars_per_page = 8000 # Limit the number of characters from each webpage to stay under the token limitwith requests.Session() as session:while start < num_results:try:15 unmodified linesverify=ssl_verify,)resp.raise_for_status()except requests.exceptions.RequestException as e:breaksoup = BeautifulSoup(resp.text, "html.parser")result_block = soup.find_all("div", attrs={"class": "g"})if not result_block:breakfor result in result_block:link = result.find("a", href=True)if link:link = link["href"]try:webpage = session.get(link, headers=headers, timeout=timeout)webpage.raise_for_status()1 unmodified lineif len(visible_text) > max_chars_per_page:visible_text = visible_text[:max_chars_per_page] + "..."all_results.append({"link": link, "text": visible_text})except requests.exceptions.RequestException as e:all_results.append({"link": link, "text": None})else:all_results.append({"link": None, "text": None})start += len(result_block)return all_resultsdef ask_question(question, temperature, top_p, repetition_penalty, web_search):147 unmodified lines14814915015115215315415515 unmodified lines1711721731741751761771781791801811821831841851861871881891901911921931 unmodified line195196197198199200201202203204205206207208209210211212213214215216217218219220147 unmodified linesall_results = []max_chars_per_page = 8000 # Limit the number of characters from each webpage to stay under the token limitprint(f"Starting Google search for term: '{term}'")with requests.Session() as session:while start < num_results:try:15 unmodified linesverify=ssl_verify,)resp.raise_for_status()print(f"Successfully retrieved search results page (start={start})")except requests.exceptions.RequestException as e:print(f"Error retrieving search results: {e}")breaksoup = BeautifulSoup(resp.text, "html.parser")result_block = soup.find_all("div", attrs={"class": "g"})if not result_block:print("No results found on this page")breakprint(f"Found {len(result_block)} results on this page")for result in result_block:link = result.find("a", href=True)if link:link = link["href"]print(f"Processing link: {link}")try:webpage = session.get(link, headers=headers, timeout=timeout)webpage.raise_for_status()1 unmodified lineif len(visible_text) > max_chars_per_page:visible_text = visible_text[:max_chars_per_page] + "..."all_results.append({"link": link, "text": visible_text})print(f"Successfully extracted text from {link}")except requests.exceptions.RequestException as e:print(f"Error retrieving webpage content: {e}")all_results.append({"link": link, "text": None})else:print("No link found for this result")all_results.append({"link": None, "text": None})start += len(result_block)print(f"Search completed. Total results: {len(all_results)}")print("Search results:")for i, result in enumerate(all_results, 1):print(f"Result {i}:")print(f" Link: {result['link']}")if result['text']:print(f" Text: {result['text'][:100]}...") # Print first 100 characterselse:print(" Text: None")print("End of search results")return all_resultsdef ask_question(question, temperature, top_p, repetition_penalty, web_search):
app.py+23