Spaces:
Running
Running
# import streamlit as st | |
# import torch | |
# from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
# import json | |
# # Load the model and tokenizer | |
# @st.cache_resource | |
# def load_model(): | |
# tokenizer = AutoTokenizer.from_pretrained('microsoft/deberta-v3-small', use_fast=False) | |
# model = AutoModelForSequenceClassification.from_pretrained("./results/checkpoint-753") | |
# model.eval() | |
# return tokenizer, model | |
# def predict_news(text, tokenizer, model): | |
# inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512) | |
# with torch.no_grad(): | |
# outputs = model(**inputs) | |
# probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1) | |
# predicted_label = torch.argmax(probabilities, dim=-1).item() | |
# confidence = probabilities[0][predicted_label].item() | |
# return "FAKE" if predicted_label == 1 else "REAL", confidence | |
# # Streamlit UI | |
# st.title("News Classifier API") | |
# # If running as an API, get the request from query parameters | |
# query_params = st.query_params | |
# if "text" in query_params: | |
# text_input = query_params["text"][0] # Get text input from URL query | |
# tokenizer, model = load_model() | |
# prediction, confidence = predict_news(text_input, tokenizer, model) | |
# # Return JSON response | |
# st.json({"prediction": prediction, "confidence": confidence}) | |
# # If running in UI mode, show text input | |
# else: | |
# text_input = st.text_area("Enter news text:") | |
# if st.button("Classify"): | |
# tokenizer, model = load_model() | |
# prediction, confidence = predict_news(text_input, tokenizer, model) | |
# st.write(f"Prediction: {prediction} (Confidence: {confidence*100:.2f}%)") | |
import streamlit as st | |
from final import * | |
import pandas as pd | |
# Page configuration | |
st.set_page_config( | |
page_title="Nexus NLP News Classifier", | |
page_icon="π°", | |
layout="wide" | |
) | |
# Cache model loading | |
def initialize_models(): | |
nlp, tokenizer, model = load_models() | |
knowledge_graph = load_knowledge_graph() | |
return nlp, tokenizer, model, knowledge_graph | |
# Initialize all models | |
nlp, tokenizer, model, knowledge_graph = initialize_models() | |
# Streamlit UI | |
def main(): | |
st.title("π° Nexus NLP News Classifier") | |
st.write("Enter news text below to analyze its authenticity") | |
# Text input area | |
news_text = st.text_area("News Text", height=200) | |
if st.button("Analyze"): | |
if news_text: | |
with st.spinner("Analyzing..."): | |
# Get predictions from all models | |
ml_prediction, ml_confidence = predict_with_model(news_text, tokenizer, model) | |
kg_prediction, kg_confidence = predict_with_knowledge_graph(text, knowledge_graph, nlp) | |
# Update knowledge graph | |
update_knowledge_graph(news_text, ml_prediction == "REAL", knowledge_graph, nlp) | |
# Get Gemini analysis | |
gemini_model = setup_gemini() | |
gemini_result = analyze_content_gemini(gemini_model, news_text) | |
# Display results in columns | |
col1, col2, col3 = st.columns(3) | |
with col1: | |
st.subheader("ML Model Analysis") | |
st.metric("Prediction", ml_prediction) | |
st.metric("Confidence", f"{ml_confidence:.2f}%") | |
with col2: | |
st.subheader("Knowledge Graph Analysis") | |
st.metric("Prediction", kg_prediction) | |
st.metric("Confidence", f"{kg_confidence:.2f}%") | |
with col3: | |
st.subheader("Gemini Analysis") | |
gemini_pred = gemini_result["gemini_analysis"]["predicted_classification"] | |
gemini_conf = gemini_result["gemini_analysis"]["confidence_score"] | |
st.metric("Prediction", gemini_pred) | |
st.metric("Confidence", f"{gemini_conf}%") | |
# Detailed analysis sections | |
with st.expander("View Detailed Analysis"): | |
st.json(gemini_result) | |
with st.expander("Named Entities"): | |
entities = extract_entities(news_text, nlp) | |
df = pd.DataFrame(entities, columns=["Entity", "Type"]) | |
st.dataframe(df) | |
else: | |
st.warning("Please enter some text to analyze") | |
if __name__ == "__main__": | |
main() | |