Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
from PIL import Image | |
from io import BytesIO | |
# Gemini API endpoint and your API key | |
GEMINI_API_ENDPOINT = "https://thegeminiapi.com/api" | |
GEMINI_API_KEY = "AIzaSyD6j1XrvNXktCFReBWo9Z5uB88fgCu6FB0" | |
# Function to detect fake news from text using Gemini API | |
def detect_fake_news_text(text): | |
try: | |
headers = { | |
"Authorization": f"Bearer {GEMINI_API_KEY}", | |
"Content-Type": "application/json" | |
} | |
data = { | |
"text": text | |
} | |
response = requests.post(f"{GEMINI_API_ENDPOINT}/text", json=data, headers=headers) | |
response.raise_for_status() # Raise exception for bad responses (4xx or 5xx) | |
result = response.json() | |
return result | |
except requests.exceptions.RequestException as e: | |
return {"error": f"Request Exception: {str(e)}"} | |
except Exception as e: | |
return {"error": f"Error: {str(e)}"} | |
# Function to detect fake news from image using Gemini API | |
def detect_fake_news_image(image): | |
try: | |
img = Image.open(BytesIO(image)) | |
img.save('temp.jpg') # Save temporary image file | |
files = {'media': open('temp.jpg', 'rb')} | |
headers = { | |
"Authorization": f"Bearer {GEMINI_API_KEY}" | |
} | |
response = requests.post(f"{GEMINI_API_ENDPOINT}/image", files=files, headers=headers) | |
response.raise_for_status() # Raise exception for bad responses (4xx or 5xx) | |
result = response.json() | |
return result | |
except requests.exceptions.RequestException as e: | |
return {"error": f"Request Exception: {str(e)}"} | |
except Exception as e: | |
return {"error": f"Error: {str(e)}"} | |
# Define Gradio interfaces | |
iface_text = gr.Interface( | |
fn=detect_fake_news_text, | |
input=gr.inputs.Textbox(lines=10, label="Enter text to analyze"), # Textbox input for text | |
outputs=gr.outputs.Textbox(label="Analysis result"), # Textbox output for result | |
title="Fake News Detection (Text)", | |
description="Detect fake news from text using Gemini API." | |
) | |
iface_image = gr.Interface( | |
fn=detect_fake_news_image, | |
inputs=gr.inputs.Image(label="Upload Image"), # Image input for uploading an image | |
outputs=gr.outputs.Textbox(label="Analysis result"), # Textbox output for result | |
title="Fake News Detection (Image)", | |
description="Detect fake news from image using Gemini API." | |
) | |
# Run Gradio interfaces | |
iface_text.launch() | |
iface_image.launch() | |