mike23415 commited on
Commit
d2e3ccd
·
verified ·
1 Parent(s): 62d43d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -71
app.py CHANGED
@@ -1,19 +1,32 @@
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>
@@ -27,46 +40,35 @@ IEEE_TEMPLATE = """
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: [your.[email protected]]
 
 
 
 
 
68
  </div>
69
-
70
  <div class="abstract">
71
  <h2>Abstract</h2>
72
  {{ abstract }}
@@ -90,41 +92,60 @@ IEEE_TEMPLATE = """
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)
 
1
+ from flask import Flask, request, send_file, jsonify
 
2
  from flask_cors import CORS
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
  import pdfkit
5
  import jinja2
6
+ import torch
7
  import tempfile
 
8
 
9
  app = Flask(__name__)
10
  CORS(app)
11
 
12
+ # Initialize model and tokenizer
13
+ try:
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ "gpt2-medium",
16
+ from_tf=False,
17
+ use_safetensors=True
18
+ )
19
+ tokenizer = AutoTokenizer.from_pretrained("gpt2-medium")
20
+ generator = pipeline(
21
+ 'text-generation',
22
+ model=model,
23
+ tokenizer=tokenizer,
24
+ device=0 if torch.cuda.is_available() else -1
25
+ )
26
+ except Exception as e:
27
+ print(f"Model loading failed: {str(e)}")
28
+ generator = None
29
 
 
30
  IEEE_TEMPLATE = """
31
  <!DOCTYPE html>
32
  <html>
 
40
  font-size: 12pt;
41
  line-height: 1.5;
42
  }
43
+ .header {
 
 
 
 
 
44
  text-align: center;
 
 
 
 
 
 
 
 
45
  margin-bottom: 24pt;
46
  }
47
+ .two-column {
48
+ column-count: 2;
49
+ column-gap: 0.5in;
 
 
 
 
 
 
 
 
 
50
  }
51
+ h1 { font-size: 14pt; margin: 12pt 0; }
52
+ h2 { font-size: 12pt; margin: 12pt 0 6pt 0; }
53
+ .abstract { margin-bottom: 24pt; }
54
+ .keywords { font-weight: bold; margin: 12pt 0; }
55
+ .references { margin-top: 24pt; }
56
+ .reference-item { text-indent: -0.5in; padding-left: 0.5in; }
57
  </style>
58
  </head>
59
  <body>
60
+ <div class="header">
61
+ <h1>{{ title }}</h1>
62
+ <div class="author-info">
63
+ {% for author in authors %}
64
+ {{ author.name }}<br>
65
+ {% if author.institution %}{{ author.institution }}<br>{% endif %}
66
+ {% if author.email %}Email: {{ author.email }}{% endif %}
67
+ {% if not loop.last %}<br>{% endif %}
68
+ {% endfor %}
69
+ </div>
70
  </div>
71
+
72
  <div class="abstract">
73
  <h2>Abstract</h2>
74
  {{ abstract }}
 
92
  </html>
93
  """
94
 
95
+ def format_content(content):
96
+ if not generator:
97
+ return content # Fallback if model failed to load
98
+
99
+ try:
100
+ prompt = f"Format this research content to IEEE standards:\n{str(content)}"
101
+ return generator(
102
+ prompt,
103
+ max_length=1024,
104
+ num_return_sequences=1,
105
+ clean_up_tokenization_spaces=True
106
+ )[0]['generated_text']
107
+ except Exception as e:
108
+ print(f"Formatting failed: {str(e)}")
109
+ return content
110
 
111
  @app.route('/generate', methods=['POST'])
112
  def generate_pdf():
113
+ try:
114
+ data = request.json
115
+ if not data or 'title' not in data or 'authors' not in data:
116
+ return jsonify({"error": "Missing required fields"}), 400
117
+
118
+ # Format content using AI
119
+ formatted = format_content(data.get('content', {}))
120
+
121
+ # Generate HTML
122
+ html = jinja2.Template(IEEE_TEMPLATE).render(
123
+ title=data['title'],
124
+ authors=data['authors'],
125
+ abstract=formatted.get('abstract', ''),
126
+ keywords=', '.join(formatted.get('keywords', [])),
127
+ sections=formatted.get('sections', []),
128
+ references=formatted.get('references', [])
129
+ )
130
+
131
+ # PDF options
132
+ options = {
133
+ 'page-size': 'Letter',
134
+ 'margin-top': '0.75in',
135
+ 'margin-right': '0.75in',
136
+ 'margin-bottom': '0.75in',
137
+ 'margin-left': '0.75in',
138
+ 'encoding': 'UTF-8',
139
+ 'quiet': ''
140
+ }
141
+
142
+ # Create PDF
143
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as f:
144
+ pdfkit.from_string(html, f.name, options=options)
145
+ return send_file(f.name, mimetype='application/pdf')
146
+
147
+ except Exception as e:
148
+ return jsonify({"error": str(e)}), 500
149
 
150
  if __name__ == '__main__':
151
  app.run(host='0.0.0.0', port=5000)