Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,54 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
# Replace with your Google
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
-
This function analyzes the text content of news using Gemini and
|
| 10 |
|
| 11 |
Args:
|
| 12 |
text: Text content of the news article
|
| 13 |
-
image: Image associated with the news article (currently unused)
|
| 14 |
|
| 15 |
Returns:
|
| 16 |
A dictionary containing insights from analysis
|
| 17 |
"""
|
| 18 |
-
analysis = {
|
| 19 |
-
sentiment_analysis = {"Overall Sentiment (Google Search)": ""}
|
| 20 |
|
| 21 |
-
#
|
| 22 |
prompt = f"Analyze the following news article for potential biases or misleading information: {text}"
|
| 23 |
-
analysis["
|
| 24 |
"gemini-pro"
|
| 25 |
-
).generate_content(prompt=prompt, api_key=
|
| 26 |
-
|
| 27 |
-
# Sentiment Analysis using Google Search (basic approach)
|
| 28 |
-
# Construct search queries to gauge sentiment from web search results
|
| 29 |
-
positive_sentiment_query = f"Is this a positive news article: {text[:100]}" # Truncate text for efficiency
|
| 30 |
-
negative_sentiment_query = f"Is this a negative news article: {text[:100]}"
|
| 31 |
-
neutral_sentiment_query = f"Is this a neutral news article: {text[:100]}"
|
| 32 |
-
|
| 33 |
-
# Use GoogleSearch library or a custom function to perform the searches (consider rate limits)
|
| 34 |
-
gs = GoogleSearch(api_key=GOOGLE_SEARCH_API_KEY) # Optional, replace with your custom search function
|
| 35 |
-
|
| 36 |
-
try:
|
| 37 |
-
positive_results = gs.search(positive_sentiment_query)
|
| 38 |
-
negative_results = gs.search(negative_sentiment_query)
|
| 39 |
-
neutral_results = gs.search(neutral_sentiment_query)
|
| 40 |
-
|
| 41 |
-
# Analyze search results to estimate sentiment (basic approach)
|
| 42 |
-
positive_count = len(list(positive_results))
|
| 43 |
-
negative_count = len(list(negative_results))
|
| 44 |
-
neutral_count = len(list(neutral_results))
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
sentiment_analysis["Overall Sentiment (Google Search)"] = "Neutral or Mixed"
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
return
|
| 57 |
|
| 58 |
|
| 59 |
# Configure Gradio interface
|
| 60 |
interface = gr.Interface(
|
| 61 |
fn=analyze_news,
|
| 62 |
-
inputs=
|
| 63 |
outputs="dict",
|
| 64 |
-
title="News Analysis with Gemini &
|
| 65 |
-
description="Analyze potential biases
|
| 66 |
)
|
| 67 |
|
| 68 |
# Launch the Gradio interface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Replace with your Google API Key
|
| 5 |
+
GOOGLE_API_KEY = "AIzaSyD6j1XrvNXktCFReBWo9Z5uB88fgCu6FB0"
|
| 6 |
|
| 7 |
+
# Replace with paths to downloaded models (download using transformers library)
|
| 8 |
+
BIAS_DETECTION_MODEL = "biasdetection/bert-base-uncased-from-sst-2-finetuned-sst-2-english"
|
| 9 |
+
SENTIMENT_ANALYSIS_MODEL = "cardiffnlp/sentiment_analysis_classifier"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def analyze_news(text):
|
| 13 |
"""
|
| 14 |
+
This function analyzes the text content of news using Gemini and pre-trained models
|
| 15 |
|
| 16 |
Args:
|
| 17 |
text: Text content of the news article
|
|
|
|
| 18 |
|
| 19 |
Returns:
|
| 20 |
A dictionary containing insights from analysis
|
| 21 |
"""
|
| 22 |
+
analysis = {}
|
|
|
|
| 23 |
|
| 24 |
+
# Gemini Analysis
|
| 25 |
prompt = f"Analyze the following news article for potential biases or misleading information: {text}"
|
| 26 |
+
analysis["Gemini Analysis"] = genai.GenerativeModel(
|
| 27 |
"gemini-pro"
|
| 28 |
+
).generate_content(prompt=prompt, api_key=GOOGLE_API_KEY).text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Bias Detection using pre-trained model
|
| 31 |
+
bias_detection = pipeline("text-classification", model=BIAS_DETECTION_MODEL)
|
| 32 |
+
bias_results = bias_detection(text)
|
| 33 |
+
analysis["Bias Detection"] = f"Predicted Bias: {bias_results[0]['label']}" \
|
| 34 |
+
f" (Confidence: {bias_results[0]['score']:.2f})"
|
|
|
|
| 35 |
|
| 36 |
+
# Sentiment Analysis using pre-trained model
|
| 37 |
+
sentiment_analysis = pipeline("sentiment-analysis", model=SENTIMENT_ANALYSIS_MODEL)
|
| 38 |
+
sentiment_results = sentiment_analysis(text)
|
| 39 |
+
analysis["Sentiment Analysis"] = f"Sentiment: {sentiment_results[0]['label']}" \
|
| 40 |
+
f" (Score: {sentiment_results[0]['score']:.2f})"
|
| 41 |
|
| 42 |
+
return analysis
|
| 43 |
|
| 44 |
|
| 45 |
# Configure Gradio interface
|
| 46 |
interface = gr.Interface(
|
| 47 |
fn=analyze_news,
|
| 48 |
+
inputs="text",
|
| 49 |
outputs="dict",
|
| 50 |
+
title="News Analysis with Gemini & Pre-trained Models",
|
| 51 |
+
description="Analyze potential biases, misleading information, and sentiment using Gemini and pre-trained models (consider limitations).",
|
| 52 |
)
|
| 53 |
|
| 54 |
# Launch the Gradio interface
|