-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
88 lines (67 loc) · 2.13 KB
/
Copy pathapp.py
File metadata and controls
88 lines (67 loc) · 2.13 KB
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
85
86
87
88
from flask import Flask, render_template, request, jsonify
from dotenv import load_dotenv
import os
from groq import Groq
from analyzer import build_description
# Load environment variables
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
raise RuntimeError("GROQ_API_KEY not found in .env file")
client = Groq(api_key=GROQ_API_KEY)
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/generate", methods=["POST"])
def generate_article():
data = request.json or {}
tweet = data.get("tweet", "").strip()
author = data.get("author", "Public Figure")
date = data.get("date", "Unknown date")
if not tweet:
return jsonify({"article": "Please enter a tweet."})
# 🔹 Your deterministic system
description = build_description(tweet, author, date)
prompt = f"""
You are a professional journalist.
Rewrite the following description into a neutral, well-written news article.
Do NOT add new facts.
Do NOT remove information.
Improve clarity and flow only.
Description:
\"\"\"{description}\"\"\"
Output format:
Headline:
Article:
Disclaimer:
"""
try:
response = client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[
{"role": "system", "content": "You rewrite text into neutral news articles."},
{"role": "user", "content": prompt}
],
temperature=0.3,
max_tokens=400
)
article = response.choices[0].message.content.strip()
return jsonify({"article": article})
except Exception as e:
print("GROQ ERROR:", e)
fallback = f"""
Headline:
{author} Shares Statement on Social Media
Article:
{description}
Disclaimer:
This article is an AI-assisted rewrite of a public tweet and does not verify or endorse the claims made.
"""
return jsonify({"article": fallback})
@app.route("/test")
def test():
return jsonify({"status": "Flask + Groq + Analyzer system is running."})
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port)