|
|
""" |
|
|
DGaze - Simplified Gradio App for Hugging Face Spaces |
|
|
""" |
|
|
|
|
|
import os |
|
|
import gradio as gr |
|
|
import requests |
|
|
import time |
|
|
import uuid |
|
|
|
|
|
|
|
|
from components.verification_result import format_verification_results |
|
|
|
|
|
|
|
|
class SpacesSettings: |
|
|
def __init__(self): |
|
|
|
|
|
self.API_BASE_URL = os.getenv("API_BASE_URL") |
|
|
if not self.API_BASE_URL: |
|
|
raise ValueError("API_BASE_URL environment variable is required") |
|
|
self.API_ENDPOINT = f"{self.API_BASE_URL}/api/verify" |
|
|
self.HEALTH_ENDPOINT = f"{self.API_BASE_URL}/api/health" |
|
|
|
|
|
|
|
|
try: |
|
|
settings = SpacesSettings() |
|
|
except ValueError as e: |
|
|
|
|
|
class DummySettings: |
|
|
API_BASE_URL = None |
|
|
API_ENDPOINT = None |
|
|
HEALTH_ENDPOINT = None |
|
|
settings = DummySettings() |
|
|
print(f"Warning: {e}") |
|
|
|
|
|
|
|
|
class SimpleAPIClient: |
|
|
def __init__(self): |
|
|
self.base_url = settings.API_BASE_URL |
|
|
self.verify_endpoint = settings.API_ENDPOINT |
|
|
self.health_endpoint = settings.HEALTH_ENDPOINT |
|
|
|
|
|
def verify_news(self, text: str) -> dict: |
|
|
"""Verify news using the backend API.""" |
|
|
try: |
|
|
payload = {"text": text} |
|
|
response = requests.post( |
|
|
self.verify_endpoint, |
|
|
json=payload, |
|
|
timeout=30, |
|
|
headers={"Content-Type": "application/json"} |
|
|
) |
|
|
response.raise_for_status() |
|
|
return response.json() |
|
|
except requests.exceptions.RequestException as e: |
|
|
raise Exception(f"API request failed: {str(e)}") |
|
|
except Exception as e: |
|
|
raise Exception(f"Verification failed: {str(e)}") |
|
|
|
|
|
api_client = SimpleAPIClient() |
|
|
|
|
|
|
|
|
trial_tracker = {} |
|
|
session_timestamps = {} |
|
|
|
|
|
def get_session_id(request) -> str: |
|
|
"""Get session ID from Gradio request.""" |
|
|
|
|
|
if hasattr(request, 'headers'): |
|
|
|
|
|
session_id = request.headers.get('x-session-id', f"trial_{uuid.uuid4().hex[:12]}") |
|
|
else: |
|
|
session_id = f"trial_{uuid.uuid4().hex[:12]}" |
|
|
return session_id |
|
|
|
|
|
def cleanup_old_sessions(): |
|
|
"""Clean up sessions older than 24 hours.""" |
|
|
current_time = time.time() |
|
|
old_sessions = [] |
|
|
for session_id, timestamp in session_timestamps.items(): |
|
|
if current_time - timestamp > 86400: |
|
|
old_sessions.append(session_id) |
|
|
|
|
|
for session_id in old_sessions: |
|
|
trial_tracker.pop(session_id, None) |
|
|
session_timestamps.pop(session_id, None) |
|
|
|
|
|
def get_trial_count(session_id: str) -> int: |
|
|
"""Get current trial verification count.""" |
|
|
cleanup_old_sessions() |
|
|
return trial_tracker.get(session_id, 0) |
|
|
|
|
|
def increment_trial_count(session_id: str) -> int: |
|
|
"""Increment and return trial verification count.""" |
|
|
current_count = trial_tracker.get(session_id, 0) |
|
|
new_count = current_count + 1 |
|
|
trial_tracker[session_id] = new_count |
|
|
session_timestamps[session_id] = time.time() |
|
|
return new_count |
|
|
|
|
|
def verify_news_with_trial_check(input_text: str, request: gr.Request) -> str: |
|
|
"""Verification function with free trial tracking for Spaces.""" |
|
|
if not input_text.strip(): |
|
|
return "Please enter some text or URL to verify" |
|
|
|
|
|
|
|
|
if not settings.API_BASE_URL: |
|
|
return """ |
|
|
<div style="background: #f8d7da; border: 1px solid #f5c6cb; padding: 1rem; margin: 1rem 0; border-radius: 4px;"> |
|
|
<p style="margin: 0; color: #721c24; font-size: 0.9rem;"> |
|
|
β οΈ <strong>Configuration Error:</strong> API_BASE_URL environment variable is not set. |
|
|
Please configure it in the Space settings. |
|
|
</p> |
|
|
</div> |
|
|
""" |
|
|
|
|
|
try: |
|
|
|
|
|
session_id = get_session_id(request) |
|
|
current_count = get_trial_count(session_id) |
|
|
|
|
|
|
|
|
if current_count >= 2: |
|
|
return """ |
|
|
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
|
|
padding: 2rem; border-radius: 12px; color: white; text-align: center; |
|
|
box-shadow: 0 10px 25px rgba(0,0,0,0.1);"> |
|
|
<h2 style="margin: 0 0 1rem 0; font-size: 1.5rem; color: white !important;">π Free Trial Limit Reached</h2> |
|
|
<p style="margin: 0 0 1.5rem 0; opacity: 0.9; color: white !important;"> |
|
|
You've used your 2 free verifications! To continue using DGaze: |
|
|
</p> |
|
|
<ul style="text-align: left; margin: 1rem 0; color: white !important;"> |
|
|
<li>Deploy your own instance with unlimited access</li> |
|
|
<li>Visit our website for more information</li> |
|
|
<li>Contact us for enterprise solutions</li> |
|
|
</ul> |
|
|
<p style="margin: 1.5rem 0 0 0; font-size: 0.9rem; opacity: 0.8; color: white !important;"> |
|
|
Thank you for trying DGaze! |
|
|
</p> |
|
|
</div> |
|
|
""" |
|
|
|
|
|
|
|
|
new_count = increment_trial_count(session_id) |
|
|
remaining = 2 - new_count |
|
|
|
|
|
|
|
|
data = api_client.verify_news(input_text) |
|
|
result = format_verification_results(data) |
|
|
|
|
|
|
|
|
trial_info = f""" |
|
|
<div style="background: #e3f2fd; border-left: 4px solid #2196f3; padding: 1rem; margin: 1rem 0; border-radius: 4px;"> |
|
|
<p style="margin: 0; color: #1565c0; font-size: 0.9rem;"> |
|
|
β‘ Free Trial: <strong>{remaining} verification{'s' if remaining != 1 else ''} remaining</strong> |
|
|
{' β’ Deploy your own instance for unlimited access!' if remaining > 0 else ''} |
|
|
</p> |
|
|
</div> |
|
|
""" |
|
|
result = trial_info + result |
|
|
return result |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error: {str(e)}" |
|
|
|
|
|
|
|
|
def create_spaces_interface(): |
|
|
"""Create the main interface for Hugging Face Spaces.""" |
|
|
with gr.Blocks( |
|
|
title="DGaze - News Verification", |
|
|
theme=gr.themes.Soft(), |
|
|
css=""" |
|
|
.main-header { |
|
|
text-align: center; |
|
|
padding: 2rem 0; |
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
|
|
color: white; |
|
|
border-radius: 12px; |
|
|
margin-bottom: 2rem; |
|
|
} |
|
|
.main-header h1 { |
|
|
color: white !important; |
|
|
font-size: 3rem; |
|
|
margin: 0; |
|
|
} |
|
|
.main-header p { |
|
|
color: white !important; |
|
|
font-size: 1.2rem; |
|
|
opacity: 0.9; |
|
|
} |
|
|
""" |
|
|
) as demo: |
|
|
|
|
|
|
|
|
gr.HTML(""" |
|
|
<div class="main-header"> |
|
|
<h1>π DGaze</h1> |
|
|
<p>Advanced News Verification System</p> |
|
|
<p style="font-size: 1rem; opacity: 0.8;">Free Trial: 2 verifications β’ Deploy your own for unlimited access</p> |
|
|
</div> |
|
|
""") |
|
|
|
|
|
|
|
|
gr.Markdown(""" |
|
|
### How it works: |
|
|
1. **Paste news text** or **URL** in the input box below |
|
|
2. **Click "Verify News"** to analyze the content |
|
|
3. **Get detailed results** including credibility score and analysis |
|
|
|
|
|
*Try our free demo with 2 verifications, then deploy your own instance for unlimited access!* |
|
|
""") |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=2): |
|
|
input_text = gr.Textbox( |
|
|
label="Enter news text or URL to verify", |
|
|
placeholder="Paste news article text or URL here...", |
|
|
lines=8, |
|
|
max_lines=15 |
|
|
) |
|
|
|
|
|
submit_btn = gr.Button("π Verify News", variant="primary", size="lg") |
|
|
|
|
|
with gr.Column(scale=3): |
|
|
output_html = gr.HTML(label="Verification Results") |
|
|
|
|
|
|
|
|
submit_btn.click( |
|
|
fn=verify_news_with_trial_check, |
|
|
inputs=input_text, |
|
|
outputs=output_html |
|
|
) |
|
|
input_text.submit( |
|
|
fn=verify_news_with_trial_check, |
|
|
inputs=input_text, |
|
|
outputs=output_html |
|
|
) |
|
|
|
|
|
|
|
|
gr.Examples( |
|
|
examples=[ |
|
|
["""BREAKING: Revolutionary AI breakthrough! π Scientists at MIT have developed a new quantum AI system that can predict earthquakes with 99.7% accuracy up to 6 months in advance. The system, called "QuakeNet AI", uses quantum computing combined with machine learning to analyze seismic patterns invisible to current technology. Dr. Sarah Chen, lead researcher, claims this could save millions of lives and prevent billions in damages. The technology will be commercially available by 2026 according to insider sources. This comes just weeks after similar breakthroughs in cancer detection AI. What do you think about this amazing discovery? #AI #Earthquake #MIT #Science"""], |
|
|
["""SpaceX conducted another major test for their Starship program yesterday, with Elon Musk claiming on social media that Flight 10 is "ready to revolutionize space travel forever." The company fired up all 33 Raptor engines on their Super Heavy booster at the Starbase facility in Texas. According to various reports and this detailed article (https://www.space.com/space-exploration/launches-spacecraft/spacex-fires-up-super-heavy-booster-ahead-of-starships-10th-test-flight-video), the test was part of preparations for the upcoming 10th test flight. However, some critics argue that SpaceX is moving too fast without proper safety protocols, especially after Flight 9 experienced issues. The FAA is still investigating the previous mission where both the booster and ship were lost. Industry experts remain divided on whether this aggressive testing schedule is beneficial or dangerous for the future of commercial spaceflight. π #SpaceX #Starship #Space"""] |
|
|
], |
|
|
inputs=[input_text], |
|
|
label="Example News Articles to Verify" |
|
|
) |
|
|
|
|
|
|
|
|
gr.Markdown(""" |
|
|
--- |
|
|
### About DGaze |
|
|
DGaze is an advanced news verification system that helps you identify potentially misleading or false information. |
|
|
|
|
|
**Features:** |
|
|
- Real-time news analysis |
|
|
- Credibility scoring |
|
|
- Source verification |
|
|
- Detailed explanations |
|
|
|
|
|
**Deploy Your Own:** |
|
|
- Get unlimited verifications |
|
|
- Customize for your needs |
|
|
- Enterprise support available |
|
|
|
|
|
--- |
|
|
*Built with β€οΈ using Gradio and Hugging Face Spaces* |
|
|
""") |
|
|
|
|
|
return demo |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo = create_spaces_interface() |
|
|
demo.launch() |