Spaces:
Sleeping
Sleeping
Download app.py from Maikobi/chatjourno_prototype: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/spaces/Maikobi/chatjourno_prototype/resolve/main/app.py
- Command line
-
hf download hf://spaces/Maikobi/chatjourno_prototype/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Maikobi/chatjourno_prototype/resolve/main/app.py
3.1 kB
| import pandas as pd | |
| import torch | |
| from sentence_transformers import SentenceTransformer, util | |
| from transformers import BartForConditionalGeneration, BartTokenizer | |
| import gradio as gr | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| sbert_model = SentenceTransformer('all-mpnet-base-v2') | |
| summarization_model = BartForConditionalGeneration.from_pretrained("facebook/bart-base") | |
| sbert_model.to(device) | |
| summarization_model.to(device) | |
| summarization_tokenizer = BartTokenizer.from_pretrained("facebook/bart-base") | |
| story_embeddings = torch.load('story_embeddings.pt', map_location=device, weights_only=True) | |
| dataset = pd.read_csv('story_data.csv') | |
| def summarize_text(text): | |
| inputs = summarization_tokenizer.encode("summarize: " + text, return_tensors="pt", max_length=512, truncation=True).to(device) | |
| summary_ids = summarization_model.generate(inputs, max_length=100, min_length=30, length_penalty=2.0, num_beams=2, early_stopping=True) | |
| return summarization_tokenizer.decode(summary_ids[0], skip_special_tokens=True) | |
| def retrieve_and_summarize_stories(query, top_n=5, similarity_threshold=0.5): | |
| query_embedding = sbert_model.encode(query, convert_to_tensor=True, device=device) | |
| similarities = util.pytorch_cos_sim(query_embedding, story_embeddings)[0] | |
| top_scores, top_indices = torch.topk(similarities, k=top_n) | |
| results = [] | |
| for score, index in zip(top_scores, top_indices): | |
| if score.item() < similarity_threshold: | |
| break | |
| story = dataset.iloc[index.item()] | |
| summary = summarize_text(story['CONTENTS']) | |
| formatted_summary = ( | |
| f"summary: {summary}\n" | |
| f"Published on: {story['SOURCE']}\n" | |
| f"Read more:{story['NEWS LINK']}\n" | |
| ) | |
| results.append(formatted_summary) | |
| return "\n\n".join(results) | |
| def interface(query, threshold): | |
| return retrieve_and_summarize_stories(query, similarity_threshold=threshold) | |
| threshold_options = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] | |
| threshold_explanation = ( | |
| "Select a similarity threshold to filter results:\n" | |
| "- **Lower values** (e.g., 0.1) will return more results, including those that are less similar to your query.\n" | |
| "- **Higher values** (e.g., 0.9) will return fewer results, focusing on those that are highly similar to your query." | |
| ) | |
| instructions = ( | |
| "How to use ChatJourno:\n" | |
| "1. Enter a query in the text box.\n" | |
| "2. Select a similarity threshold from the dropdown menu.\n" | |
| "3. Click the 'Submit' button to retrieve and summarize similar news stories." | |
| ) | |
| iface = gr.Interface( | |
| fn=interface, | |
| inputs=[ | |
| gr.components.Textbox(label="Query", lines=2, placeholder="Enter your query here..."), | |
| gr.components.Dropdown( | |
| choices=threshold_options, | |
| label="Similarity Threshold", | |
| value=0.5, | |
| type="value" | |
| ), | |
| ], | |
| outputs="json", | |
| title="ChatJourno", | |
| description=instructions + "\n\n" + threshold_explanation | |
| ) | |
| iface.launch() |