Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporating Images in Article #74

Open
apix7 opened this issue Feb 12, 2025 · 0 comments
Open

Incorporating Images in Article #74

apix7 opened this issue Feb 12, 2025 · 0 comments

Comments

@apix7
Copy link

apix7 commented Feb 12, 2025

We can seamlessly incorporate several relevant images into the generated article by taking advantage of free services. I experimented with DuckDuckGo search—it’s free and unlimited, and I was impressed with the quality.

Image

code might look like something like this :

class DDGS:
    ...

    def images(self, keywords: str, max_results: int = config.DEFAULT_IMAGE_RESULTS):
        vqd = self._get_vqd(keywords)
        payload = {
            "q": keywords,
            "vqd": vqd,
            "o": "json",
            "f": ",,",
            "p": "-2",
            "ex": "-2"
        }
        results = []
        try:
            for page in range(3):
                payload["p"] = str(page + 1)
                resp_content = self._get_url("GET", "https://duckduckgo.com/i.js", params=payload)
                data = json.loads(resp_content)
                
                for item in data.get("results", []):
                    results.append({
                        "title": item.get("title", ""),
                        "image": item.get("image", ""),
                        "thumbnail": item.get("thumbnail", ""),
                        "source": item.get("source", ""),
                        "height": item.get("height", ""),
                        "width": item.get("width", "")
                    })
                    if len(results) >= max_results:
                        return results
                
                if not data.get("results"):
                    break
                    
            return results
        except Exception as e:
            logger.warning(f"Error fetching image results: {str(e)}")
            return []

with a prompt like :

"Please choose few images that best illustrate the content of the article. In your final article, insert the chosen images using the format ![caption] (URL) at appropriate locations where they enhance the narrative.\n"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant