mike23415 commited on
Commit
827b95e
·
verified ·
1 Parent(s): 710ce5d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -0
app.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ from flask import Flask, request, send_file
3
+ from flask_cors import CORS
4
+ from transformers import pipeline
5
+ import pdfkit
6
+ import jinja2
7
+ import tempfile
8
+ import os
9
+
10
+ app = Flask(__name__)
11
+ CORS(app)
12
+
13
+ # Initialize Hugging Face pipeline with open-source model
14
+ formatter = pipeline('text-generation', model='gpt2-medium')
15
+
16
+ # IEEE Template Configuration
17
+ IEEE_TEMPLATE = """
18
+ <!DOCTYPE html>
19
+ <html>
20
+ <head>
21
+ <meta charset="UTF-8">
22
+ <title>{{ title }}</title>
23
+ <style>
24
+ @page { margin: 0.75in; }
25
+ body {
26
+ font-family: 'Times New Roman', Times, serif;
27
+ font-size: 12pt;
28
+ line-height: 1.5;
29
+ }
30
+ .two-column {
31
+ column-count: 2;
32
+ column-gap: 0.5in;
33
+ }
34
+ h1 {
35
+ font-size: 14pt;
36
+ text-align: center;
37
+ margin: 12pt 0;
38
+ }
39
+ h2 {
40
+ font-size: 12pt;
41
+ margin: 12pt 0 6pt 0;
42
+ }
43
+ .abstract {
44
+ column-count: 1;
45
+ margin-bottom: 24pt;
46
+ }
47
+ .keywords {
48
+ font-weight: bold;
49
+ margin: 12pt 0;
50
+ }
51
+ .references {
52
+ column-count: 1;
53
+ margin-top: 24pt;
54
+ }
55
+ .reference-item {
56
+ text-indent: -0.5in;
57
+ padding-left: 0.5in;
58
+ margin: 6pt 0;
59
+ }
60
+ </style>
61
+ </head>
62
+ <body>
63
+ <h1>{{ title }}</h1>
64
+ <div class="author-info">
65
+ Priyanshu Chauhan<br>
66
+ University School of Automation & Robotics<br>
67
+ Email: [[email protected]]
68
+ </div>
69
+
70
+ <div class="abstract">
71
+ <h2>Abstract</h2>
72
+ {{ abstract }}
73
+ <div class="keywords">Keywords— {{ keywords }}</div>
74
+ </div>
75
+
76
+ <div class="two-column">
77
+ {% for section in sections %}
78
+ <h2>{{ section.title }}</h2>
79
+ {{ section.content }}
80
+ {% endfor %}
81
+ </div>
82
+
83
+ <div class="references">
84
+ <h2>References</h2>
85
+ {% for ref in references %}
86
+ <div class="reference-item">[{{ loop.index }}] {{ ref }}</div>
87
+ {% endfor %}
88
+ </div>
89
+ </body>
90
+ </html>
91
+ """
92
+
93
+ def format_ieee(content):
94
+ """Enhance content to IEEE standards using AI"""
95
+ prompt = f"Convert this research content to strict IEEE format:\n{content}"
96
+ return formatter(prompt, max_length=1024)[0]['generated_text']
97
+
98
+ @app.route('/generate', methods=['POST'])
99
+ def generate_pdf():
100
+ data = request.json
101
+
102
+ # Process content through AI formatter
103
+ formatted = format_ieee(data['content'])
104
+
105
+ # Create PDF with strict IEEE formatting
106
+ html = jinja2.Template(IEEE_TEMPLATE).render(
107
+ title=data['title'],
108
+ abstract=formatted.get('abstract', ''),
109
+ keywords=', '.join(formatted.get('keywords', [])),
110
+ sections=formatted.get('sections', []),
111
+ references=formatted.get('references', [])
112
+ )
113
+
114
+ # PDF configuration for IEEE standards
115
+ options = {
116
+ 'page-size': 'Letter',
117
+ 'margin-top': '0.75in',
118
+ 'margin-right': '0.75in',
119
+ 'margin-bottom': '0.75in',
120
+ 'margin-left': '0.75in',
121
+ 'encoding': 'UTF-8',
122
+ 'quiet': ''
123
+ }
124
+
125
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as f:
126
+ pdfkit.from_string(html, f.name, options=options)
127
+ return send_file(f.name, mimetype='application/pdf')
128
+
129
+ if __name__ == '__main__':
130
+ app.run(host='0.0.0.0', port=5000)