File size: 2,992 Bytes
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
# app.py
import gradio as gr
import requests
from bs4 import BeautifulSoup
import google.generativeai as genai
from newspaper import Article
import os

# Configure Gemini API
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=GEMINI_API_KEY)

def fetch_article_content(url):
    """Extract article content from URL"""
    try:
        # Using newspaper3k for better article extraction
        article = Article(url)
        article.download()
        article.parse()
        return article.text
    except Exception as e:
        return f"Error fetching article: {str(e)}"

def generate_platform_post(article_text):
    """Generate optimized post using Gemini API"""
    try:
        model = genai.GenerativeModel('gemini-1.5-pro')
        
        prompt = f"""
        Analyze this article content and create:
        1. A compelling title (max 100 characters)
        2. An optimized post in HTML format for Reddit/Quora
        3. Include an image tag with descriptive alt text
        
        Article content:
        {article_text[:5000]}  # Limit to 5000 chars for token limits
        
        Format your response as:
        [TITLE]
        [HTML_CONTENT]
        
        Requirements:
        - Clean HTML formatting with paragraphs
        - Add relevant image tag with descriptive alt text
        - Mobile-friendly design
        - Minimal CSS styling
        """
        
        response = model.generate_content(prompt)
        return parse_gemini_response(response.text)
    except Exception as e:
        return {"title": "Error generating post", "content": f"<p>{str(e)}</p>"}

def parse_gemini_response(response):
    """Parse Gemini's response into title and content"""
    try:
        title = response.split("[TITLE]")[1].split("[HTML_CONTENT]")[0].strip()
        content = response.split("[HTML_CONTENT]")[1].strip()
    except:
        title = "Content Generation Error"
        content = "<p>Failed to parse response from AI</p>"
    
    return {"title": title, "content": content}

def process_url(url):
    """Main processing pipeline"""
    article_text = fetch_article_content(url)
    if article_text.startswith("Error"):
        return {"title": "Processing Error", "content": f"<p>{article_text}</p>"}
    
    return generate_platform_post(article_text)

# Create Gradio interface
url_input = gr.Textbox(label="Article URL", placeholder="https://example.com/article...")
title_output = gr.Textbox(label="Generated Title")
content_output = gr.HTML(label="Generated 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 Platform Post Converter",
    description="Convert news articles into optimized Reddit/Quora-style posts with AI-generated formatting and image descriptions"
)

if __name__ == "__main__":
    app.launch()