Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,46 +9,29 @@ GEMINI_API_KEY = "AIzaSyD6j1XrvNXktCFReBWo9Z5uB88fgCu6FB0"
|
|
| 9 |
|
| 10 |
# Function to detect fake news from text using Gemini API
|
| 11 |
def detect_fake_news_text(text):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Function to detect fake news from image using Gemini API
|
| 24 |
def detect_fake_news_image(image):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# Define Gradio interfaces
|
| 36 |
-
iface_text = gr.Interface(
|
| 37 |
-
fn=detect_fake_news_text,
|
| 38 |
-
inputs=gr.Textbox(lines=10, label="Enter text to analyze"),
|
| 39 |
-
outputs=gr.Textbox(label="Analysis result"),
|
| 40 |
-
title="Fake News Detection (Text)",
|
| 41 |
-
description="Detect fake news from text using Gemini API."
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
iface_image = gr.Interface(
|
| 45 |
-
fn=detect_fake_news_image,
|
| 46 |
-
inputs=gr.Image(shape=(224, 224)),
|
| 47 |
-
outputs=gr.Textbox(label="Analysis result"),
|
| 48 |
-
title="Fake News Detection (Image)",
|
| 49 |
-
description="Detect fake news from image using Gemini API."
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
# Run Gradio interfaces
|
| 53 |
-
iface_text.launch()
|
| 54 |
-
iface_image.launch()
|
|
|
|
| 9 |
|
| 10 |
# Function to detect fake news from text using Gemini API
|
| 11 |
def detect_fake_news_text(text):
|
| 12 |
+
try:
|
| 13 |
+
headers = {
|
| 14 |
+
"Authorization": f"Bearer {GEMINI_API_KEY}",
|
| 15 |
+
"Content-Type": "application/json"
|
| 16 |
+
}
|
| 17 |
+
data = {
|
| 18 |
+
"text": text
|
| 19 |
+
}
|
| 20 |
+
response = requests.post(f"{GEMINI_API_ENDPOINT}/text", json=data, headers=headers)
|
| 21 |
+
response.raise_for_status() # Raise exception for bad responses (4xx or 5xx)
|
| 22 |
+
result = response.json()
|
| 23 |
+
return result
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return {"error": str(e)}
|
| 26 |
|
| 27 |
# Function to detect fake news from image using Gemini API
|
| 28 |
def detect_fake_news_image(image):
|
| 29 |
+
try:
|
| 30 |
+
img = Image.open(BytesIO(image))
|
| 31 |
+
img.save('temp.jpg') # Save temporary image file
|
| 32 |
+
files = {'media': open('temp.jpg', 'rb')}
|
| 33 |
+
headers = {
|
| 34 |
+
"Authorization": f"Bearer {GEMINI_API_KEY}"
|
| 35 |
+
}
|
| 36 |
+
response = requests.post(f"{GEMINI_API_ENDPOINT}/image", files=files, headers=headers)
|
| 37 |
+
response.raise_for_status() # Raise exception for bad responses
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|