Spaces:
Sleeping
Sleeping
File size: 3,648 Bytes
2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a 0a52165 ea8f6b3 0a52165 ea8f6b3 0a52165 ea8f6b3 0a52165 ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a ea8f6b3 2bd4e6a 0a52165 ea8f6b3 2bd4e6a |
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
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() |