Commit ·
d34735d
1
Parent(s): 897c7ba
update file 013
Browse files- app.py +103 -22
- templates/index.html +4 -0
- templates/tickets.html +64 -0
app.py
CHANGED
|
@@ -2,55 +2,108 @@ from flask import Flask, request, jsonify, send_file, render_template
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from langchain.prompts import PromptTemplate
|
| 4 |
from reportlab.pdfgen import canvas
|
|
|
|
| 5 |
import os
|
| 6 |
import uuid
|
| 7 |
-
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
-
# Load
|
| 12 |
-
|
| 13 |
-
generator = pipeline("text-generation", model="gpt2", device=-1)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Prompt template
|
| 17 |
template = PromptTemplate(
|
| 18 |
-
input_variables=["
|
| 19 |
template="""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# Directories
|
| 31 |
-
os.makedirs("logs", exist_ok=True)
|
| 32 |
-
os.makedirs("pdfs", exist_ok=True)
|
| 33 |
-
|
| 34 |
@app.route("/")
|
| 35 |
def home():
|
| 36 |
return render_template("index.html")
|
| 37 |
|
| 38 |
@app.route("/generate", methods=["POST"])
|
| 39 |
def generate_ticket():
|
| 40 |
-
|
|
|
|
| 41 |
email = request.form.get("email")
|
| 42 |
issue = request.form.get("issue")
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
result = generator(prompt, max_length=200, do_sample=True)[0]["generated_text"]
|
| 46 |
|
| 47 |
session_id = str(uuid.uuid4())
|
| 48 |
-
timestamp = datetime.now().strftime("%Y-%m-%
|
| 49 |
|
|
|
|
| 50 |
log_path = f"logs/{session_id}.txt"
|
| 51 |
with open(log_path, "w") as f:
|
| 52 |
f.write(f"Timestamp: {timestamp}\n")
|
| 53 |
-
f.write(f"Name: {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
return render_template("index.html", ticket=result, session_id=session_id)
|
| 56 |
|
|
@@ -75,5 +128,33 @@ def download_pdf(session_id):
|
|
| 75 |
|
| 76 |
return send_file(pdf_path, as_attachment=True)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
if __name__ == "__main__":
|
| 79 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from langchain.prompts import PromptTemplate
|
| 4 |
from reportlab.pdfgen import canvas
|
| 5 |
+
from datetime import datetime
|
| 6 |
import os
|
| 7 |
import uuid
|
| 8 |
+
import sqlite3
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
+
# Load model (CPU)
|
| 13 |
+
generator = pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B", device=-1)
|
|
|
|
| 14 |
|
| 15 |
+
# Directories
|
| 16 |
+
os.makedirs("logs", exist_ok=True)
|
| 17 |
+
os.makedirs("pdfs", exist_ok=True)
|
| 18 |
+
os.makedirs("data", exist_ok=True)
|
| 19 |
+
|
| 20 |
+
# DB setup
|
| 21 |
+
DB_PATH = "data/tickets.db"
|
| 22 |
+
|
| 23 |
+
def init_db():
|
| 24 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 25 |
+
cursor = conn.cursor()
|
| 26 |
+
cursor.execute('''
|
| 27 |
+
CREATE TABLE IF NOT EXISTS tickets (
|
| 28 |
+
id TEXT PRIMARY KEY,
|
| 29 |
+
first_name TEXT,
|
| 30 |
+
last_name TEXT,
|
| 31 |
+
email TEXT,
|
| 32 |
+
issue TEXT,
|
| 33 |
+
priority TEXT,
|
| 34 |
+
date TEXT,
|
| 35 |
+
generated TEXT,
|
| 36 |
+
created_at TEXT
|
| 37 |
+
)
|
| 38 |
+
''')
|
| 39 |
+
conn.commit()
|
| 40 |
+
|
| 41 |
+
init_db()
|
| 42 |
|
| 43 |
# Prompt template
|
| 44 |
template = PromptTemplate(
|
| 45 |
+
input_variables=["first_name", "last_name", "email", "date", "priority", "issue"],
|
| 46 |
template="""
|
| 47 |
+
You are an HR support assistant. Generate a professional HR helpdesk ticket.
|
| 48 |
+
|
| 49 |
+
Employee Details:
|
| 50 |
+
- First Name: {first_name}
|
| 51 |
+
- Last Name: {last_name}
|
| 52 |
+
- Email: {email}
|
| 53 |
+
- Issue Date: {date}
|
| 54 |
+
- Priority: {priority}
|
| 55 |
+
- Reported Issue: {issue}
|
| 56 |
+
|
| 57 |
+
Write a clear, concise ticket title and description suitable for HR review.
|
| 58 |
+
"""
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
@app.route("/")
|
| 62 |
def home():
|
| 63 |
return render_template("index.html")
|
| 64 |
|
| 65 |
@app.route("/generate", methods=["POST"])
|
| 66 |
def generate_ticket():
|
| 67 |
+
first_name = request.form.get("firstName")
|
| 68 |
+
last_name = request.form.get("lastName")
|
| 69 |
email = request.form.get("email")
|
| 70 |
issue = request.form.get("issue")
|
| 71 |
+
priority = request.form.get("priority")
|
| 72 |
+
date = request.form.get("date")
|
| 73 |
+
|
| 74 |
+
prompt = template.format(
|
| 75 |
+
first_name=first_name,
|
| 76 |
+
last_name=last_name,
|
| 77 |
+
email=email,
|
| 78 |
+
date=date,
|
| 79 |
+
priority=priority,
|
| 80 |
+
issue=issue
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
result = generator(prompt, max_length=200, do_sample=True)[0]["generated_text"]
|
| 84 |
|
| 85 |
session_id = str(uuid.uuid4())
|
| 86 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 87 |
|
| 88 |
+
# Save log file
|
| 89 |
log_path = f"logs/{session_id}.txt"
|
| 90 |
with open(log_path, "w") as f:
|
| 91 |
f.write(f"Timestamp: {timestamp}\n")
|
| 92 |
+
f.write(f"First Name: {first_name}\nLast Name: {last_name}\n")
|
| 93 |
+
f.write(f"Email: {email}\nDate: {date}\nPriority: {priority}\n")
|
| 94 |
+
f.write(f"Issue: {issue}\n\nGenerated:\n{result}\n")
|
| 95 |
+
|
| 96 |
+
# Save to SQLite DB
|
| 97 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 98 |
+
cursor = conn.cursor()
|
| 99 |
+
cursor.execute('''
|
| 100 |
+
INSERT INTO tickets (id, first_name, last_name, email, issue, priority, date, generated, created_at)
|
| 101 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
| 102 |
+
''', (
|
| 103 |
+
session_id, first_name, last_name, email,
|
| 104 |
+
issue, priority, date, result, timestamp
|
| 105 |
+
))
|
| 106 |
+
conn.commit()
|
| 107 |
|
| 108 |
return render_template("index.html", ticket=result, session_id=session_id)
|
| 109 |
|
|
|
|
| 128 |
|
| 129 |
return send_file(pdf_path, as_attachment=True)
|
| 130 |
|
| 131 |
+
@app.route("/tickets")
|
| 132 |
+
def view_tickets():
|
| 133 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 134 |
+
cursor = conn.cursor()
|
| 135 |
+
cursor.execute('''
|
| 136 |
+
SELECT id, first_name, last_name, email, priority, date, generated
|
| 137 |
+
FROM tickets
|
| 138 |
+
ORDER BY created_at DESC
|
| 139 |
+
''')
|
| 140 |
+
rows = cursor.fetchall()
|
| 141 |
+
|
| 142 |
+
# Convert to list of dicts for easy templating
|
| 143 |
+
tickets = [
|
| 144 |
+
{
|
| 145 |
+
"id": row[0],
|
| 146 |
+
"first_name": row[1],
|
| 147 |
+
"last_name": row[2],
|
| 148 |
+
"email": row[3],
|
| 149 |
+
"priority": row[4],
|
| 150 |
+
"date": row[5],
|
| 151 |
+
"summary": (row[6][:100] + '...') if len(row[6]) > 100 else row[6]
|
| 152 |
+
}
|
| 153 |
+
for row in rows
|
| 154 |
+
]
|
| 155 |
+
|
| 156 |
+
return render_template("tickets.html", tickets=tickets)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
if __name__ == "__main__":
|
| 160 |
app.run(host="0.0.0.0", port=7860)
|
templates/index.html
CHANGED
|
@@ -64,5 +64,9 @@
|
|
| 64 |
</div>
|
| 65 |
{% endif %}
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
</body>
|
| 68 |
</html>
|
|
|
|
| 64 |
</div>
|
| 65 |
{% endif %}
|
| 66 |
|
| 67 |
+
<p style="text-align: center;">
|
| 68 |
+
<a href="/tickets">📄 View Submitted Tickets</a>
|
| 69 |
+
</p>
|
| 70 |
+
|
| 71 |
</body>
|
| 72 |
</html>
|
templates/tickets.html
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Submitted Tickets</title>
|
| 5 |
+
<style>
|
| 6 |
+
body {
|
| 7 |
+
font-family: Arial, sans-serif;
|
| 8 |
+
padding: 40px;
|
| 9 |
+
background-color: #f9f9f9;
|
| 10 |
+
}
|
| 11 |
+
table {
|
| 12 |
+
width: 100%;
|
| 13 |
+
border-collapse: collapse;
|
| 14 |
+
margin-top: 30px;
|
| 15 |
+
background: white;
|
| 16 |
+
}
|
| 17 |
+
th, td {
|
| 18 |
+
padding: 12px;
|
| 19 |
+
text-align: left;
|
| 20 |
+
border-bottom: 1px solid #ddd;
|
| 21 |
+
}
|
| 22 |
+
th {
|
| 23 |
+
background: #007bff;
|
| 24 |
+
color: white;
|
| 25 |
+
}
|
| 26 |
+
a.button {
|
| 27 |
+
text-decoration: none;
|
| 28 |
+
padding: 8px 15px;
|
| 29 |
+
background: #28a745;
|
| 30 |
+
color: white;
|
| 31 |
+
border-radius: 6px;
|
| 32 |
+
}
|
| 33 |
+
</style>
|
| 34 |
+
</head>
|
| 35 |
+
<body>
|
| 36 |
+
<h2>📋 Submitted Tickets</h2>
|
| 37 |
+
<table>
|
| 38 |
+
<thead>
|
| 39 |
+
<tr>
|
| 40 |
+
<th>First</th>
|
| 41 |
+
<th>Last</th>
|
| 42 |
+
<th>Email</th>
|
| 43 |
+
<th>Priority</th>
|
| 44 |
+
<th>Issue Date</th>
|
| 45 |
+
<th>Summary</th>
|
| 46 |
+
<th>PDF</th>
|
| 47 |
+
</tr>
|
| 48 |
+
</thead>
|
| 49 |
+
<tbody>
|
| 50 |
+
{% for ticket in tickets %}
|
| 51 |
+
<tr>
|
| 52 |
+
<td>{{ ticket.first_name }}</td>
|
| 53 |
+
<td>{{ ticket.last_name }}</td>
|
| 54 |
+
<td>{{ ticket.email }}</td>
|
| 55 |
+
<td>{{ ticket.priority }}</td>
|
| 56 |
+
<td>{{ ticket.date }}</td>
|
| 57 |
+
<td>{{ ticket.summary }}</td>
|
| 58 |
+
<td><a class="button" href="/download/{{ ticket.id }}">Download</a></td>
|
| 59 |
+
</tr>
|
| 60 |
+
{% endfor %}
|
| 61 |
+
</tbody>
|
| 62 |
+
</table>
|
| 63 |
+
</body>
|
| 64 |
+
</html>
|