import os from src.images.Search_Image.search import find_similar_img_from_url import re import gradio as gr from src.images.Search_Image.image_model_share import ( image_generation_detection, ) from src.texts.Search_Text._text_detection_share import ( UNKNOWN, abstract_detect_generated_text, ) from src.texts.Search_Text.fake_text_generation_share import ( highlight_overlap_by_word_to_list, ) os.environ["no_proxy"] = "localhost,127.0.0.1,::1" TEMP_IMAGE = "temp_image.jpg" TEMP_INPUT_IMAGE = "temp_input_image.jpg" HUMAN_IMAGE = "data/test_data/human_news.jpg" HUMAN_CAPTION = "Stoke City have secured West Brom striker Saido Berahino for £12 million on a five-and-a-half-year contract." HUMAN_CONTENT = """ Tracey Jolliffe has already donated a kidney, 16 eggs and 80 pints of blood, and intends to leave her brain to science. She is now hoping to give away part of her liver to a person she may never meet. "If I had another spare kidney, I'd do it again," Tracey tells the BBC's Victoria Derbyshire programme. She is what is known as an "altruistic donor" - someone willing to give away an organ to potentially help save the life of a complete stranger. A microbiologist in the NHS, and the daughter of two nurses, she has spent her life learning about the importance of healthcare from a professional standpoint. But she has also been keen to make a difference on a personal level. "I signed up to donate blood, and to the bone marrow register, when I was 18," she says. Now 50, her wish to donate has become gradually more expansive. In 2012, she was one of fewer than 100 people that year to donate a kidney without knowing the recipient's identity - and now supports the charity Give A Kidney, encouraging others to do the same. As of 30 September 2016, 5,126 people remain on the NHS kidney transplant waiting list. Tracey's kidney donation, in all likelihood, will have saved someone's life. "I remind myself of it every day when I wake up," she says, rightly proud of her life-changing actions. It was not, however, a decision taken on the spur of a moment. Donating a kidney is an "involved process", she says, with suitability assessments taking at least three months to complete. Tests leading up to the transplant include X-rays, heart tracing and a special test of kidney function, which involves an injection and a series of blood tests. "It is not something to do if you're scared of needles," she jokes. The risks associated with donating, however, are relatively low for those deemed healthy enough to proceed, with a mortality rate of about one in 3,000 - roughly the same as having an appendix removed. Compared with the general public, NHS Blood and Transplant says, most kidney donors have equivalent - or better - life expectancy than the average person. Tracey says she was in hospital for five days after her operation but felt "back to normal" within six weeks. """ HUMAN_NEWS_CNN = """ Mayotte authorities fear hunger and disease after cyclone, as death toll rises in Mozambique Cyclone Chido caused devastation in Mayotte and authorities are now rushing to prevent disease and hunger spreading in the French overseas territory Sipa USA Authorities in Mayotte were racing on Tuesday to stop hunger, disease and lawlessness from spreading in the French overseas territory after the weekend’s devastating cyclone, while Mozambique reported dozens of deaths from the storm. Hundreds or even thousands could be dead in Mayotte, which took the strongest hit from Cyclone Chido, French officials have said. The storm laid waste to large parts of the archipelago off east Africa, France’s poorest overseas territory, before striking continental Africa. With many parts of Mayotte still inaccessible and some victims buried before their deaths could be officially counted, it may take days to discover the full extent of the destruction. So far, 22 deaths and more than 1,400 injuries have been confirmed, Ambdilwahedou Soumaila, the mayor of the capital Mamoudzou, told Radio France Internationale on Tuesday morning. “The priority today is water and food,” Soumaila said. “There are people who have unfortunately died where the bodies are starting to decompose that can create a sanitary problem.” “We don’t have electricity. When night falls, there are people who take advantage of that situation.” Rescue workers operate in storm-hit Mayotte on Wednesday. Rescue workers operate in storm-hit Mayotte on Wednesday. Securite Civile via Reuters Twenty tonnes of food and water are due to start arriving on Tuesday by air and sea. The French government said late on Monday it expects 50% of water supplies to be restored within 48 hours and 95% within the week. France’s interior ministry announced that a curfew would go into effect on Tuesday night from 10 p.m. to 4 a.m. local time. Rescue workers have been searching for survivors amid the debris of shantytowns bowled over by 200 kph (124 mph) winds. Chido was the strongest storm to strike Mayotte in more than 90 years, French weather service Meteo France said. In Mozambique, it killed at least 34 people, officials said on Tuesday. Another seven died in Malawi. Drone footage from Mozambique’s Cabo Delgado province, already experiencing a humanitarian crisis due to an Islamist insurgency, showed razed thatched-roof houses near the beach and personal belongings scattered under the few palm trees still standing. Dispute over immigration French President Emmanuel Macron said after an emergency cabinet meeting on Monday that he would visit Mayotte in the coming days, as the disaster quickly fueled a political back-and-forth about immigration, the environment and France’s treatment of its overseas territories. Mayotte has been grappling with unrest in recent years, with many residents angry at illegal immigration and inflation. More than three-quarters of its roughly 321,000 people live in relative poverty, and about one-third are estimated to be undocumented migrants, most from nearby Comoros and Madagascar. The territory has become a stronghold for the far-right National Rally with 60% voting for Marine Le Pen in the 2022 presidential election runoff. France’s acting Interior Minister Bruno Retailleau, from the conservative Republicans party, told a news conference in Mayotte that the early warning system had worked “perfectly” but many of the undocumented had not come to designated shelters. People stand amid uprooted trees and debris after cyclone Chido hit Mecufi district, Cabo Delgado province, Mozambique, on December 16. People stand amid uprooted trees and debris after cyclone Chido hit Mecufi district, Cabo Delgado province, Mozambique, on December 16. UNICEF Mozambique via Reuters Other officials have said undocumented migrants may have been afraid to go to shelters for fear of being arrested. The toll of the cyclone, Retailleau said in a later post on X, underscored the need to address “the migration question.” “Mayotte is the symbol of the drift that (French) governments have allowed to take hold on this issue,” he said. “We will need to legislate so that in Mayotte, like everywhere else on the national territory, France retakes control of its immigration.” Left-wing politicians, however, have pointed the finger at what they say is the government’s neglect of Mayotte and failure to prepare for natural disasters linked to climate change. Socialist Party chairman Olivier Faure blasted Retailleau’s comments in an X post. “He could have interrogated the role of climate change in producing more and more intense climate disasters. He could have rallied against the extreme poverty that makes people more vulnerable to cyclones,” said Faure. “No, he has resumed his crusade against migrants.” Prime Minister Francois Bayrou, appointed last week to steer France out of a political crisis, faced criticism after he went to the town of Pau, where he is the mayor, to attend a municipal council meeting on Monday, instead of visiting Mayotte. """ HUMAN_NEWS_CNN_IMAGE = "human_cnn.webp" # generate a short news related to sport # opposite OPPOSITE_NEWS = """ Tracey Jolliffe has never donated a kidney, any eggs, or blood, and has no plans to leave her brain to science. She is not considering giving away any part of her liver to someone she knows. "If I had another spare kidney, I wouldn't do it again," Tracey tells the BBC's Victoria Derbyshire programme. She is not an "altruistic donor" - someone unwilling to give away an organ to potentially save the life of a complete stranger. A microbiologist outside the NHS, with parents who were not in healthcare, she has spent her life without focusing on the importance of healthcare from a professional standpoint. She has also not been eager to make a difference on a personal level. "I never signed up to donate blood, nor to the bone marrow register, when I was 18," she says. Now 50, her interest in donating has not expanded. In 2012, she was not among the few people that year to donate a kidney without knowing the recipient's identity - and does not support the charity Give A Kidney, discouraging others from doing the same. As of 30 September 2016, 5,126 people remain on the NHS kidney transplant waiting list. Tracey's decision not to donate a kidney hasn't saved anyone's life. "I never think about it when I wake up," she says, indifferent about her choices. It was not a decision made after careful consideration. Donating a kidney is not an "involved process", she says, with suitability assessments taking less than three months to complete. Tests leading up to the transplant do not include X-rays, heart tracing, or a special test of kidney function, which does not involve an injection or any blood tests. "It is something to do if you're scared of needles," she jokes. The risks associated with donating, however, are relatively high for those not deemed healthy enough to proceed, with a high mortality rate - much greater than having an appendix removed. Compared with the general public, NHS Blood and Transplant says, most kidney donors have worse life expectancy than the average person. Tracey says she was not in hospital after any operation and did not feel "back to normal" within six weeks. """ PARAPHASE_NEWS = """ Tracey Jolliffe has generously donated a kidney, 16 eggs, and 80 pints of blood, and plans to donate her brain to science. She now hopes to donate part of her liver to someone she may never meet. "If I had another spare kidney, I'd do it again," she shares with the BBC's Victoria Derbyshire program. Known as an "altruistic donor," Tracey is willing to donate organs to help save the lives of strangers. As a microbiologist in the NHS and the daughter of two nurses, Tracey has always understood the importance of healthcare professionally. However, she also strives to make a personal impact. "I signed up to donate blood and joined the bone marrow register at 18," she explains. Now 50, her desire to donate has expanded over the years. In 2012, Tracey was among fewer than 100 people that year who donated a kidney without knowing the recipient. She now supports Give A Kidney, a charity that encourages others to donate. As of 30 September 2016, 5,126 people were on the NHS kidney transplant waiting list. Tracey's kidney donation likely saved a life. "I remind myself of it every day when I wake up," she says, proud of her life-changing decision. Donating a kidney was not a spontaneous decision for Tracey. It is a complex process, she explains, with suitability assessments taking at least three months. Pre-transplant tests include X-rays, heart monitoring, and a special kidney function test involving an injection and multiple blood tests. "It's not for those afraid of needles," she jokes. For healthy individuals, the risks of donating a kidney are relatively low, with a mortality rate of about one in 3,000, similar to having an appendix removed. According to NHS Blood and Transplant, most kidney donors have the same or better life expectancy compared to the general population. Tracey was hospitalized for five days after her operation and felt "back to normal" within six weeks. """ MACHINE_IMAGE = "data/test_data/machine_news.png" # MACHINE_CAPTION = "Argentina Secures Victory in Thrilling Friendly Match Against Brazil" MACHINE_CONTENT = """ Tracey Jolliffe has already donated a kidney, 16 eggs, and 80 pints of blood, and she intends to leave her brain to science. She is now hoping to give away part of her liver to a person she may never meet. "If I had another spare kidney, I'd do it again," Tracey tells the BBC's Victoria Derbyshire programme. She is what is known as an "altruistic donor"—someone willing to give away an organ to potentially help save the life of a complete stranger. A microbiologist in the NHS and the daughter of two nurses, she has spent her life learning about the importance of healthcare from a professional standpoint. But she has also been keen to make a difference on a personal level. "I signed up to donate blood and to the bone marrow register when I was 18," she says. Now 50, her wish to donate has become gradually more expansive. In 2012, she was one of fewer than 100 people that year to donate a kidney without knowing the recipient's identity, and she now supports the charity Give A Kidney, encouraging others to do the same. As of 30 September 2016, 5,126 people remain on the NHS kidney transplant waiting list. Tracey's kidney donation, in all likelihood, has saved someone's life. "I remind myself of it every day when I wake up," she says, rightly proud of her life-changing actions. It was not, however, a decision taken on the spur of a moment. Donating a kidney is an "involved process," she says, with suitability assessments taking at least three months to complete. Tests leading up to the transplant include X-rays, heart tracing, and a special test of kidney function, which involves an injection and a series of blood tests. "It is not something to do if you're scared of needles," she jokes. The risks associated with donating, however, are relatively low for those deemed healthy enough to proceed, with a mortality rate of about one in 3,000—roughly the same as having an appendix removed. Compared with the general public, NHS Blood and Transplant says, most kidney donors have equivalent—or better—life expectancy than the average person. Tracey says she was in hospital for five days after her operation but felt "back to normal" within six weeks. """ HUMAN_BBC_NEWS2 = """ A message of hope at Washington march For such a divisive figure, Donald Trump managed to unify hundreds of thousands of Americans at the Women's March on Washington. Moments after Mr Trump was sworn in as the 45th president on Friday, he delivered a thundering speech in which he promised to improve the lives of millions of Americans. A day later, throngs of women, men and children streamed into the same area where he made that pledge, in order to take a stand for gender and racial equality. Though Mr Trump's named was mentioned frequently, the march, which organisers estimate attracted more than half a million, was not only about the new US president. Messages ranged from "Thank you for making me an activist Trump" to "We will not be silenced," but the common thread throughout the patchwork of signs was hope. "It's about solidarity and visualising the resistance," said Jonathon Meier, who took a bus from New York. "And I think it not only helps with the healing process, but it gives me hope for the next four years." A sea of activists, some clad in knitted, pink "pussy" hats and others draped in American flags, ambled about the National Mall, stopping to catch a glimpse of some of the high-profile speakers and singing along to songs like "This Little Light of Mine". Peppered among the many protest signs were images of ovaries and female genitals, a nod to concerns over losing access to birth control and abortion care under a Trump administration. """ FREELY_GENERATION_NEWS = """ A new study has indicated that criminals and terrorists are increasingly turning to the dark net to purchase weapons. The study, conducted by cybersecurity firm Recorded Future, found that these purchases are being made anonymously and with cryptocurrency, making it difficult for law enforcement agencies to track and intercept them. The dark net is a hidden part of the internet, accessible only through anonymous browsers, where users can buy and sell a variety of illegal goods and services. However, the study found that weapons purchases are becoming more popular on the dark net, with firearms and explosives being the most commonly traded items. Recorded Future's research showed that many of the weapons being sold on the dark net are military-grade, and the study suggests that this is due to the large number of surplus weapons available following military conflicts in various parts of the world. The report also found that the sellers on the dark net are often located in countries with lax gun laws, leading to concerns that these weapons could end up in the hands of criminals and terrorists who could use them to commit acts of violence. The use of cryptocurrency to purchase these weapons adds another layer of difficulty for law enforcement agencies trying to track down those responsible. The anonymity provided by cryptocurrency allows buyers and sellers to conduct their transactions without leaving a trace. The findings of this study serve as a stark reminder of the dangers posed by the dark net, and the need for law enforcement agencies to remain vigilant in their efforts to combat illegal activity on this hidden part of the internet. """ HUMAN_BBC_NEWS2_IMAGE = "human_bbc_news_2.webp" HIGHLIGHT = "highlight" def highlight_text(words, indexes): final_words = words for index in indexes: final_words[index] = ( f"{words[index]}" ) return " ".join(final_words) def format_pair(pair): input_sentence = highlight_text(pair[0], pair[2]) source_sentence = highlight_text(pair[1], pair[3]) return f"{input_sentence}{source_sentence}" def create_table(data): table_rows = "\n".join([format_pair(pair) for pair in data]) return f"""
Comparison between input news and source news at the above link
{table_rows}
Input sentence Source sentence
""" with gr.Blocks() as demo: image = gr.Image( value=HUMAN_IMAGE, label="News Image", height=200, width=200, type="filepath", ) content = gr.Textbox(label="Content", lines=3, value=HUMAN_CONTENT) process_btn = gr.Button("Process") """ 1. human bbc news 2. proofreading 3. opposite 4. human bbc news 2 5. human_cnn news 6. paraphrase 7. freely generation """ gr.Examples( examples=[ [HUMAN_IMAGE, HUMAN_CONTENT], [MACHINE_IMAGE, MACHINE_CONTENT], [MACHINE_IMAGE, OPPOSITE_NEWS], [HUMAN_BBC_NEWS2_IMAGE, HUMAN_BBC_NEWS2], [HUMAN_NEWS_CNN_IMAGE, HUMAN_NEWS_CNN], [MACHINE_IMAGE, PARAPHASE_NEWS], [MACHINE_IMAGE, FREELY_GENERATION_NEWS], ], inputs=[image, content], label="examples", example_labels=[ "human bbc news", "proofreading", "opposite", "human bbc news 2", "human cnn news", "paraphrase", "freely generation", ], ) overall = gr.HTML() matching_html = gr.HTML() def process(input_image, content): ( search_engine_prediction, SOTA_prediction, SOTA_confidence, found_url, sentence_pairs, ) = abstract_detect_generated_text(content) final_table = [] COLOR_MAPS = { "HUMAN": "", "MACHINE": "", } source_image = [] image_prediction_label, image_confidence = image_generation_detection( input_image, ) # [found_img_url, image_different_score] = find_similar_img_from_url(input_image) # if 0 < image_different_score < 10: # search_engine_description = f'Most likely generated by {COLOR_MAPS["HUMAN"]} (score = {image_different_score}) with evidence link at {found_img_url} ' # else: # TODO add < 25 which is cropped images # search_engine_description = f'Most likely generated by {COLOR_MAPS["MACHINE"]} (score = {image_different_score})' for ( input_sentence, source_sentence, check_paraphrase, ) in sentence_pairs: input_words, source_words, input_indexes, source_indexes = ( highlight_overlap_by_word_to_list( input_sentence, source_sentence, ) ) final_table.append( (input_words, source_words, input_indexes, source_indexes), ) if search_engine_prediction == UNKNOWN: search_engine_description = "Cannot find any evidence link" final_prediction = SOTA_prediction else: final_prediction = search_engine_prediction search_engine_description = f'Most likely generated by {COLOR_MAPS[search_engine_prediction]}{search_engine_prediction} with evidence link at {found_url} ' overall_html_result = f"""

Image generation detection


Text generation detection

 

""" if len(final_table) != 0: html_table = create_table(final_table) else: html_table = "" return overall_html_result, html_table process_btn.click( process, inputs=[image, content], outputs=[overall, matching_html], ) demo.launch(share=False)