Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,60 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
# Replace with your actual Google API Key
|
| 4 |
-
GOOGLE_API_KEY = "
|
|
|
|
| 5 |
|
| 6 |
def analyze_news(text, image):
|
| 7 |
"""
|
| 8 |
-
This function analyzes the text and image content of news using Gemini
|
| 9 |
-
|
| 10 |
-
**Note:** This is a simplified example and doesn't determine real or fake news.
|
| 11 |
|
| 12 |
Args:
|
| 13 |
text: Text content of the news article
|
| 14 |
image: Image associated with the news article (optional)
|
| 15 |
|
| 16 |
Returns:
|
| 17 |
-
A dictionary containing insights from
|
| 18 |
"""
|
|
|
|
|
|
|
| 19 |
# Call Gemini with text prompt
|
| 20 |
prompt = f"Analyze the following news article for potential biases or misleading information: {text}"
|
| 21 |
-
analysis
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Additional analysis for image (if provided)
|
| 24 |
if image:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
return {"Analysis": analysis}
|
| 30 |
|
| 31 |
# Configure Gradio interface
|
| 32 |
interface = gr.Interface(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
)
|
| 39 |
|
| 40 |
# Launch the Gradio interface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests # for external API calls
|
| 3 |
|
| 4 |
# Replace with your actual Google API Key
|
| 5 |
+
GOOGLE_API_KEY = "YOUR_API_KEY"
|
| 6 |
+
|
| 7 |
|
| 8 |
def analyze_news(text, image):
|
| 9 |
"""
|
| 10 |
+
This function analyzes the text and image content of news using Gemini and external APIs
|
|
|
|
|
|
|
| 11 |
|
| 12 |
Args:
|
| 13 |
text: Text content of the news article
|
| 14 |
image: Image associated with the news article (optional)
|
| 15 |
|
| 16 |
Returns:
|
| 17 |
+
A dictionary containing insights from analysis
|
| 18 |
"""
|
| 19 |
+
analysis = {"Text Analysis (Gemini)": ""}
|
| 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_API_KEY).text
|
| 26 |
|
| 27 |
# Additional analysis for image (if provided)
|
| 28 |
if image:
|
| 29 |
+
analysis["Image Analysis (External API)"] = "Image analysis not implemented yet. Here's an example using an external API:"
|
| 30 |
+
|
| 31 |
+
# Example using a fake news detection API (replace with a reputable API)
|
| 32 |
+
# Replace 'YOUR_FAKE_NEWS_API_KEY' with your actual key
|
| 33 |
+
fake_news_api_url = "https://fake-news-api.com/api/v1/classify?text=" # Replace with a valid API
|
| 34 |
+
fake_news_api_key = "YOUR_FAKE_NEWS_API_KEY"
|
| 35 |
+
headers = {"Authorization": f"Bearer {fake_news_api_key}"}
|
| 36 |
+
image_data = {"image": image.getvalue()} # Convert image to bytes
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
response = requests.post(
|
| 40 |
+
fake_news_api_url + text, headers=headers, files=image_data
|
| 41 |
+
)
|
| 42 |
+
response_json = response.json()
|
| 43 |
+
analysis["Image Analysis (External API)"] += f"\n - Prediction: {response_json['prediction']}"
|
| 44 |
+
analysis["Image Analysis (External API)"] += f"\n - Confidence: {response_json['confidence']}"
|
| 45 |
+
except Exception as e:
|
| 46 |
+
analysis["Image Analysis (External API)"] += f"\n - Error: {e}"
|
| 47 |
+
|
| 48 |
+
return analysis
|
| 49 |
|
|
|
|
| 50 |
|
| 51 |
# Configure Gradio interface
|
| 52 |
interface = gr.Interface(
|
| 53 |
+
fn=analyze_news,
|
| 54 |
+
inputs=[gr.Textbox("News Article Text"), gr.Image(type="pil")],
|
| 55 |
+
outputs="dict",
|
| 56 |
+
title="News Analysis with Gemini & External APIs",
|
| 57 |
+
description="Analyze potential biases, misleading information, and (for images) predict 'fake news' using Gemini and external APIs (consider limitations).",
|
| 58 |
)
|
| 59 |
|
| 60 |
# Launch the Gradio interface
|