Atchyuteswar commited on
Commit
fb3d34c
·
verified ·
1 Parent(s): 74b597c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -42
app.py CHANGED
@@ -1,68 +1,54 @@
1
  import gradio as gr
2
- from google.search import GoogleSearch
3
 
4
- # Replace with your Google Search API Key (optional, for higher quota)
5
- GOOGLE_SEARCH_API_KEY = None
6
 
7
- def analyze_news(text, image):
 
 
 
 
 
8
  """
9
- This function analyzes the text content of news using Gemini and Google Search
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 = {"Text Analysis (Gemini)": ""}
19
- sentiment_analysis = {"Overall Sentiment (Google Search)": ""}
20
 
21
- # Call Gemini with text prompt
22
  prompt = f"Analyze the following news article for potential biases or misleading information: {text}"
23
- analysis["Text Analysis (Gemini)"] = genai.GenerativeModel(
24
  "gemini-pro"
25
- ).generate_content(prompt=prompt, api_key=GOOGLE_SEARCH_API_KEY).text
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
- if positive_count > negative_count and positive_count > neutral_count:
47
- sentiment_analysis["Overall Sentiment (Google Search)"] = "Positive"
48
- elif negative_count > positive_count and negative_count > neutral_count:
49
- sentiment_analysis["Overall Sentiment (Google Search)"] = "Negative"
50
- else:
51
- sentiment_analysis["Overall Sentiment (Google Search)"] = "Neutral or Mixed"
52
 
53
- except Exception as e:
54
- sentiment_analysis["Overall Sentiment (Google Search)"] = f"Error: {e}"
 
 
 
55
 
56
- return {**analysis, **sentiment_analysis}
57
 
58
 
59
  # Configure Gradio interface
60
  interface = gr.Interface(
61
  fn=analyze_news,
62
- inputs=[gr.Textbox("News Article Text"), gr.Image(type="pil")],
63
  outputs="dict",
64
- title="News Analysis with Gemini & Google Search",
65
- description="Analyze potential biases and misleading information using Gemini. Estimate sentiment using Google Search (consider limitations).",
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