Commit ·
0c729b9
1
Parent(s): 3f21d3a
update file 007
Browse files- app.py +55 -33
- requirements.txt +1 -1
- templates/index.html +56 -40
app.py
CHANGED
|
@@ -1,55 +1,77 @@
|
|
| 1 |
-
from flask import Flask,
|
| 2 |
-
from transformers import pipeline
|
| 3 |
from langchain.prompts import PromptTemplate
|
| 4 |
-
from
|
| 5 |
import os
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
-
# Load lightweight
|
| 11 |
-
generator = pipeline(
|
| 12 |
-
set_seed(42)
|
| 13 |
|
| 14 |
-
# Prompt
|
| 15 |
template = PromptTemplate(
|
| 16 |
-
input_variables=["issue"],
|
| 17 |
-
template="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
@app.route(
|
| 25 |
def generate_ticket():
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
with open(
|
| 34 |
-
f.write(f"
|
|
|
|
| 35 |
|
| 36 |
-
return
|
| 37 |
|
| 38 |
-
@app.route(
|
| 39 |
-
def download_pdf():
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
pdf.add_page()
|
| 43 |
-
pdf.set_font("Arial", size=12)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
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 |
from datetime import datetime
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
+
# Load lightweight model for CPU
|
| 12 |
+
generator = pipeline("text-generation", model="tiiuae/falcon-7b-instruct", device=-1)
|
|
|
|
| 13 |
|
| 14 |
+
# Prompt template
|
| 15 |
template = PromptTemplate(
|
| 16 |
+
input_variables=["issue", "name", "email"],
|
| 17 |
+
template="""
|
| 18 |
+
You are an HR support assistant. Generate a professional HR helpdesk ticket based on the issue.
|
| 19 |
+
|
| 20 |
+
Employee Name: {name}
|
| 21 |
+
Email: {email}
|
| 22 |
+
Reported Issue: {issue}
|
| 23 |
+
|
| 24 |
+
Provide a ticket title and description.
|
| 25 |
+
"""
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Directories
|
| 29 |
+
os.makedirs("logs", exist_ok=True)
|
| 30 |
+
os.makedirs("pdfs", exist_ok=True)
|
| 31 |
+
|
| 32 |
+
@app.route("/")
|
| 33 |
+
def home():
|
| 34 |
+
return render_template("index.html")
|
| 35 |
|
| 36 |
+
@app.route("/generate", methods=["POST"])
|
| 37 |
def generate_ticket():
|
| 38 |
+
name = request.form.get("name")
|
| 39 |
+
email = request.form.get("email")
|
| 40 |
+
issue = request.form.get("issue")
|
| 41 |
+
|
| 42 |
+
prompt = template.format(name=name, email=email, issue=issue)
|
| 43 |
+
result = generator(prompt, max_length=200, do_sample=True)[0]["generated_text"]
|
| 44 |
|
| 45 |
+
session_id = str(uuid.uuid4())
|
| 46 |
+
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 47 |
|
| 48 |
+
log_path = f"logs/{session_id}.txt"
|
| 49 |
+
with open(log_path, "w") as f:
|
| 50 |
+
f.write(f"Timestamp: {timestamp}\n")
|
| 51 |
+
f.write(f"Name: {name}\nEmail: {email}\nIssue: {issue}\n\nGenerated:\n{result}\n")
|
| 52 |
|
| 53 |
+
return render_template("index.html", ticket=result, session_id=session_id)
|
| 54 |
|
| 55 |
+
@app.route("/download/<session_id>")
|
| 56 |
+
def download_pdf(session_id):
|
| 57 |
+
log_path = f"logs/{session_id}.txt"
|
| 58 |
+
pdf_path = f"pdfs/{session_id}.pdf"
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
if not os.path.exists(log_path):
|
| 61 |
+
return jsonify({"error": "Session not found"}), 404
|
| 62 |
|
| 63 |
+
with open(log_path, "r") as f:
|
| 64 |
+
content = f.read()
|
| 65 |
|
| 66 |
+
c = canvas.Canvas(pdf_path)
|
| 67 |
+
lines = content.splitlines()
|
| 68 |
+
y = 800
|
| 69 |
+
for line in lines:
|
| 70 |
+
c.drawString(50, y, line)
|
| 71 |
+
y -= 15
|
| 72 |
+
c.save()
|
| 73 |
|
| 74 |
+
return send_file(pdf_path, as_attachment=True)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
flask
|
| 2 |
transformers
|
| 3 |
torch
|
| 4 |
-
fpdf
|
| 5 |
langchain
|
|
|
|
|
|
| 1 |
flask
|
| 2 |
transformers
|
| 3 |
torch
|
|
|
|
| 4 |
langchain
|
| 5 |
+
reportlab
|
templates/index.html
CHANGED
|
@@ -1,52 +1,68 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
<head>
|
| 4 |
-
<title>HR Ticket Generator</title>
|
| 5 |
<style>
|
| 6 |
-
body {
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
</style>
|
| 10 |
</head>
|
| 11 |
<body>
|
| 12 |
-
<h2>HR Ticket Generator</h2>
|
| 13 |
-
<form id="ticketForm">
|
| 14 |
-
<input type="text" name="query" placeholder="Enter your HR issue..." required />
|
| 15 |
-
<button type="submit">Generate Ticket</button>
|
| 16 |
-
</form>
|
| 17 |
-
<textarea id="result" readonly></textarea>
|
| 18 |
-
<button id="downloadBtn" style="display:none;">Download as PDF</button>
|
| 19 |
|
| 20 |
-
|
| 21 |
-
const form = document.getElementById('ticketForm');
|
| 22 |
-
const resultBox = document.getElementById('result');
|
| 23 |
-
const downloadBtn = document.getElementById('downloadBtn');
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
downloadBtn.onclick = async () => {
|
| 38 |
-
const ticket = resultBox.value;
|
| 39 |
-
const formData = new FormData();
|
| 40 |
-
formData.append('ticket', ticket);
|
| 41 |
-
const res = await fetch('/download_pdf', {
|
| 42 |
-
method: 'POST',
|
| 43 |
-
body: formData
|
| 44 |
-
});
|
| 45 |
-
const file = await res.json();
|
| 46 |
-
if (file.success) {
|
| 47 |
-
window.location.href = file.file;
|
| 48 |
-
}
|
| 49 |
-
};
|
| 50 |
-
</script>
|
| 51 |
</body>
|
| 52 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
<head>
|
| 4 |
+
<title>AI HR Ticket Generator</title>
|
| 5 |
<style>
|
| 6 |
+
body {
|
| 7 |
+
font-family: Arial, sans-serif;
|
| 8 |
+
padding: 40px;
|
| 9 |
+
background-color: #f0f2f5;
|
| 10 |
+
}
|
| 11 |
+
form {
|
| 12 |
+
background: white;
|
| 13 |
+
padding: 30px;
|
| 14 |
+
border-radius: 10px;
|
| 15 |
+
box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.1);
|
| 16 |
+
max-width: 600px;
|
| 17 |
+
margin: auto;
|
| 18 |
+
}
|
| 19 |
+
input, textarea {
|
| 20 |
+
width: 100%;
|
| 21 |
+
padding: 12px;
|
| 22 |
+
margin: 10px 0 20px;
|
| 23 |
+
border: 1px solid #ccc;
|
| 24 |
+
border-radius: 8px;
|
| 25 |
+
}
|
| 26 |
+
button {
|
| 27 |
+
background-color: #007bff;
|
| 28 |
+
border: none;
|
| 29 |
+
padding: 12px 25px;
|
| 30 |
+
border-radius: 8px;
|
| 31 |
+
color: white;
|
| 32 |
+
font-size: 16px;
|
| 33 |
+
}
|
| 34 |
+
.ticket-box {
|
| 35 |
+
background: #e6ffe6;
|
| 36 |
+
padding: 20px;
|
| 37 |
+
margin-top: 30px;
|
| 38 |
+
border-radius: 10px;
|
| 39 |
+
}
|
| 40 |
</style>
|
| 41 |
</head>
|
| 42 |
<body>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
<h2 align="center">🤖 AI HR Ticket Generator</h2>
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
<form action="/generate" method="post">
|
| 47 |
+
<label for="name">Employee Name:</label>
|
| 48 |
+
<input type="text" name="name" required>
|
| 49 |
+
|
| 50 |
+
<label for="email">Email:</label>
|
| 51 |
+
<input type="email" name="email" required>
|
| 52 |
+
|
| 53 |
+
<label for="issue">Issue Description:</label>
|
| 54 |
+
<textarea name="issue" rows="5" required></textarea>
|
| 55 |
+
|
| 56 |
+
<button type="submit">Generate Ticket</button>
|
| 57 |
+
</form>
|
| 58 |
+
|
| 59 |
+
{% if ticket %}
|
| 60 |
+
<div class="ticket-box">
|
| 61 |
+
<h3>Generated Ticket:</h3>
|
| 62 |
+
<pre>{{ ticket }}</pre>
|
| 63 |
+
<a href="/download/{{ session_id }}"><button>Download as PDF</button></a>
|
| 64 |
+
</div>
|
| 65 |
+
{% endif %}
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
</body>
|
| 68 |
</html>
|