Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
from bs4 import BeautifulSoup | |
import google.generativeai as genai | |
import os | |
import time | |
# Configure Gemini API | |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") | |
if not GEMINI_API_KEY: | |
raise ValueError("GEMINI_API_KEY not found in environment variables") | |
genai.configure(api_key=GEMINI_API_KEY) | |
def fetch_article_content(url): | |
"""Fetch article content with timeout and fallback""" | |
try: | |
headers = {'User-Agent': 'Mozilla/5.0'} | |
response = requests.get(url, headers=headers, timeout=8) # 8s timeout | |
response.raise_for_status() | |
soup = BeautifulSoup(response.text, 'html.parser') | |
# Extract text from <p> tags | |
paragraphs = soup.find_all('p')[:10] # Limit to 10 paragraphs | |
content = ' '.join([p.get_text(strip=True) for p in paragraphs]) | |
return content[:2000] # Limit to 2000 chars for prompt safety | |
except Exception as e: | |
return f"Error fetching article: {str(e)}" | |
def generate_platform_post(article_text): | |
"""Generate optimized post using Gemini API with timeout""" | |
try: | |
model = genai.GenerativeModel('gemini-1.5-pro') | |
prompt = f""" | |
Convert this article into a short, engaging post suitable for Reddit or Quora. | |
Article content: | |
{article_text} | |
Output format: | |
Title: [short title] | |
Post: [clean HTML with paragraphs and one image tag] | |
Requirements: | |
- Keep title under 100 characters | |
- Include one <img> tag with alt text | |
- Use minimal styling | |
- Mobile-friendly | |
- No markdown | |
""" | |
# Timeout mechanism | |
start_time = time.time() | |
response = model.generate_content(prompt) | |
print(f"AI Response received in {time.time()-start_time:.2f} seconds") | |
return parse_gemini_response(response.text) | |
except Exception as e: | |
return {"title": "Generation Failed", "content": f"<p>AI Error: {str(e)}</p>"} | |
def parse_gemini_response(response): | |
"""Robust parsing with fallbacks""" | |
try: | |
title = response.split("Title:")[1].split("Post:")[0].strip()[:100] | |
content = response.split("Post:")[1].strip() | |
except: | |
title = "Content Generation Error" | |
content = "<p>Failed to parse AI response</p>" | |
return {"title": title, "content": content} | |
def process_url(url): | |
"""Main processing pipeline with status updates""" | |
yield {"title": "Fetching Article...", "content": "<p>Connecting to URL...</p>"} | |
article_text = fetch_article_content(url) | |
if article_text.startswith("Error"): | |
yield {"title": "Fetch Error", "content": f"<p>{article_text}</p>"} | |
return | |
yield {"title": "Generating Post...", "content": "<p>Creating content with AI...</p>"} | |
result = generate_platform_post(article_text) | |
yield result | |
# Gradio Interface | |
url_input = gr.Textbox(label="Article URL", placeholder="https://example.com/article...", lines=1) | |
title_output = gr.Textbox(label="Generated Title") | |
content_output = gr.HTML(label="Formatted Post") | |
app = gr.Interface( | |
fn=process_url, | |
inputs=url_input, | |
outputs=[ | |
gr.Textbox(label="Generated Title"), | |
gr.HTML(label="Formatted Post") | |
], | |
examples=[ | |
["https://example.com/sample-article"] | |
], | |
title="Article to Reddit/Quora Post Converter", | |
description="Convert news articles into optimized posts with AI-generated formatting and image descriptions", | |
allow_flagging="never", | |
live=False | |
) | |
if __name__ == "__main__": | |
app.launch() |