Spaces:
Running
Running
| from flask import Flask, render_template, request | |
| from dotenv import load_dotenv | |
| from groq import Groq | |
| import os | |
| import markdown | |
| # Load env variables | |
| load_dotenv() | |
| app = Flask(__name__) | |
| # Initialize Groq client | |
| client = Groq( | |
| api_key=os.getenv("GROQ_API_KEY") | |
| ) | |
| def generate_blog(topic): | |
| prompt = f""" | |
| Write a detailed blog article about: {topic} | |
| Include: | |
| 1. Title | |
| 2. Introduction | |
| 3. Main Content | |
| 4. Conclusion | |
| """ | |
| try: | |
| completion = client.chat.completions.create( | |
| model="llama-3.1-8b-instant", | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": prompt | |
| } | |
| ], | |
| temperature=0.7, | |
| max_tokens=700, | |
| ) | |
| generated_text = completion.choices[0].message.content | |
| print("GENERATED:") | |
| print(generated_text) | |
| return generated_text | |
| except Exception as e: | |
| print("ERROR:") | |
| print(str(e)) | |
| return f"Error: {str(e)}" | |
| def index(): | |
| paragraph = "" | |
| error = "" | |
| if request.method == "POST": | |
| topic = request.form.get("topic", "").strip() | |
| if topic: | |
| result = generate_blog(topic) | |
| if result.startswith("Error"): | |
| error = result | |
| else: | |
| paragraph = markdown.markdown(result) | |
| return render_template( | |
| "index.html", | |
| paragraph=paragraph, | |
| error=error | |
| ) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) |