File size: 2,473 Bytes
e6191f1
f1ad8ed
 
 
 
 
 
 
 
 
 
787f942
 
 
 
 
 
 
 
 
 
 
 
ccd0bab
 
787f942
ccd0bab
f1ad8ed
 
 
787f942
 
 
 
 
 
 
 
ccd0bab
 
 
 
 
 
 
 
 
 
 
7d1da90
b00c0c3
ccd0bab
 
 
 
 
 
b00c0c3
 
ccd0bab
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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()