Spaces:
Running
Running
File size: 1,575 Bytes
fb946d7 ec7fa23 fb946d7 9bcd95c fb946d7 ec7fa23 fb946d7 70f8402 ec7fa23 ee43bf8 fb946d7 70f8402 4ba12a3 c486e29 4ba12a3 c486e29 ec7fa23 c486e29 58921ad ec7fa23 e1c982a ec7fa23 d3c4072 c486e29 d3c4072 70f8402 c486e29 58921ad c486e29 d3c4072 4ba12a3 c486e29 58921ad a962133 fb946d7 4ba12a3 fb946d7 1650d46 70f8402 fb946d7 4ba12a3 70f8402 a962133 4ba12a3 70f8402 1650d46 b4f043d fb946d7 70f8402 a962133 4ba12a3 fb946d7 ec7fa23 | 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 | 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)}"
@app.route("/", methods=["GET", "POST"])
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) |