Skip to content
site logo mobile

Forum in maintenance, we will back soon 🙂

instagram post gena...
 
Notifications
Clear all

instagram post genatate tool error, Fast API

7 Posts
3 Users
1 Reactions
8 Views
dimu
 dimu
(@dassa)
Posts: 87
Member
Power Member
Pythonista Prodigy Badge
Prompt Engineer
Topic starter
 

Mr Hassan, I start to create a tool to generate different Instagram posts using live blog posts. So my Idea is to read the blog post from the given link and change that blog post according to the given Instagram content type. I use ChatGPT to generate this. but it gives me an error. please check the below code...

Sample prompt for testing:

generate_instagram_posts = """As an expert social media content creator, your task is to analyze the given blog post content: {blog_content} and generate Instagram posts. Each post should be tailored to the specified content type: {content_type}. The output should be ONLY valid JSON as follows:
[
  {{
    "content_type": "{content_type}",
    "content": "Generated Instagram post content based on the blog content."
  }}
]

The JSON object:\n\n
"""
class InstagramPost(BaseModel):
    content_type: str
    content: str

class InstagramPostGeneratorResponse(BaseModel):
    success: bool
    message: str
    posts: list[InstagramPost]

@router.get("/instagram/generate")
async def generate_instagram_posts(url: str, content_type: str):
    start_time = time.time()
    max_retries = 5

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
    }

    for retry_count in range(max_retries):
        try:
            response = requests.get(url, headers=headers)
            if response.status_code != 200:
                logger.error(f"Failed to fetch blog post content, HTTP status code: {response.status_code}")
                raise HTTPException(status_code=response.status_code, detail="Failed to fetch blog post content")

            blog_content = response.text

            if await llm.is_text_flagged(blog_content) or await llm.is_text_flagged(content_type):
                return {
                    "success": False,
                    "message": "Input Not Allowed",
                    "result": None
                }

            prompt = pr.generate_instagram_posts.format(blog_content=blog_content, content_type=content_type)
            # Assuming llm.generate_with_response_model generates Instagram posts directly
            # Replace with appropriate logic based on llm functionality
            generated_posts = await llm.generate_with_response_model(prompt, 3, InstagramPostGeneratorResponse)

            if generated_posts:
                return {
                    "success": True,
                    "message": "Generated Instagram Posts Successfully",
                    "result": generated_posts.posts
                }
            else:
                raise Exception("Failed to generate Instagram posts")

        except requests.exceptions.RequestException as e:
            logger.error(f"An HTTP error occurred: {e}")
            raise HTTPException(status_code=500, detail=f"An HTTP error occurred: {e}")

        except (json.JSONDecodeError, ValidationError) as e:
            logger.warning(f"Failed during JSON decoding or validation. Retry count: {retry_count + 1}. Error: {e}")

        except KeyError as e:
            logger.warning(f"Missing key in JSON: {e}")

        except Exception as e:
            logger.error(e)
            continue
        finally:
            elapsed_time = time.time() - start_time
            # do something with the elapsed time

    return {
        "success": False,
        "message": "Failed to generate Instagram posts",
        "result": None
    }
 
Posted : 06/23/2024 2:48 am
Topic Tags
SSAdvisor
(@ssadvisor)
Posts: 1063
Noble Member
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@dassa what's the error?

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 06/23/2024 12:42 pm
Hasan Aboul Hasan
(@admin)
Posts: 1103
Member Admin
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

sorry, what is the error? 

did you run locally?

is this my template?

 
Posted : 06/24/2024 5:00 pm
dimu
 dimu
(@dassa)
Posts: 87
Member
Power Member
Pythonista Prodigy Badge
Prompt Engineer
Topic starter
 

Yes, this is your template. and I did run locally.

 
1.2
This post was modified 1 week ago 2 times by dimu
 
Posted : 06/26/2024 2:29 am
Hasan Aboul Hasan
(@admin)
Posts: 1103
Member Admin
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

I see an error in the Mongo connection. Do you have a Mongo database?

 
Posted : 06/26/2024 3:25 am
dimu
 dimu
(@dassa)
Posts: 87
Member
Power Member
Pythonista Prodigy Badge
Prompt Engineer
Topic starter
 

@admin Actually no. I'm confused about Mongodb. I registered with their account that's all.

 
Posted : 06/27/2024 2:23 am
Hasan Aboul Hasan
(@admin)
Posts: 1103
Member Admin
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@dassa if you dont want to use Mongo, then no need to use in code. just remove the connection.

 
Posted : 06/27/2024 2:09 pm
dimu reacted
Share: