Atchyuteswar commited on
Commit
787f942
·
verified ·
1 Parent(s): f1ad8ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -40
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
- headers = {
13
- "Authorization": f"Bearer {GEMINI_API_KEY}",
14
- "Content-Type": "application/json"
15
- }
16
- data = {
17
- "text": text
18
- }
19
- response = requests.post(f"{GEMINI_API_ENDPOINT}/text", json=data, headers=headers)
20
- result = response.json()
21
- return result
 
 
 
 
22
 
23
  # Function to detect fake news from image using Gemini API
24
  def detect_fake_news_image(image):
25
- img = Image.open(BytesIO(image))
26
- img.save('temp.jpg') # Save temporary image file
27
- files = {'media': open('temp.jpg', 'rb')}
28
- headers = {
29
- "Authorization": f"Bearer {GEMINI_API_KEY}"
30
- }
31
- response = requests.post(f"{GEMINI_API_ENDPOINT}/image", files=files, headers=headers)
32
- result = response.json()
33
- return result
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