Spaces:
Runtime error
Runtime error
Download app.py from SohamSSarkar/postpilot: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/spaces/SohamSSarkar/postpilot/resolve/main/app.py
- Command line
-
hf download hf://spaces/SohamSSarkar/postpilot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SohamSSarkar/postpilot/resolve/main/app.py
1.45 kB
| import os | |
| import gradio as gr | |
| from dotenv import load_dotenv | |
| from openai import OpenAI | |
| from transformers import pipeline | |
| load_dotenv() | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| # Setup HF pipeline for fallback (runs inference in Hugging Face's cloud) | |
| hf_generator = pipeline("text-generation", model="gpt2") | |
| def generate_post(idea): | |
| if not idea: | |
| return "Please enter an idea." | |
| prompt = f""" | |
| You are PostPilot, an AI that turns an idea into a LinkedIn-ready post. | |
| Return the post in 2-3 short paragraphs, add 4-6 hashtags, and mention it was built with AI in under 1 hour. | |
| Idea: {idea} | |
| """ | |
| try: | |
| # Try OpenAI first | |
| resp = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[{"role": "user", "content": prompt}], | |
| temperature=0.3 | |
| ) | |
| return resp.choices[0].message.content | |
| except Exception as e: | |
| # If OpenAI fails (e.g. quota exceeded), fallback to HF model | |
| fallback_text = hf_generator(prompt, max_length=150, num_return_sequences=1)[0]["generated_text"] | |
| return f"β οΈ OpenAI failed, showing fallback:\n\n" + fallback_text | |
| demo = gr.Interface( | |
| fn=generate_post, | |
| inputs="text", | |
| outputs="text", | |
| title="PostPilot β AI LinkedIn Post Generator", | |
| description="Paste an idea, get a ready-to-post LinkedIn article with hashtags." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |