Clean up code and remove debug prints
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, request, render_template
|
| 2 |
from transformers import AutoModelForTokenClassification, RobertaTokenizer, pipeline
|
| 3 |
from collections import Counter
|
| 4 |
import datetime, json
|
|
@@ -16,6 +16,7 @@ except Exception as e:
|
|
| 16 |
print(f"Error loading pipeline: {str(e)}")
|
| 17 |
raise
|
| 18 |
|
|
|
|
| 19 |
@app.route("/", methods=["GET", "POST"])
|
| 20 |
def index():
|
| 21 |
predictions = []
|
|
@@ -35,9 +36,17 @@ def index():
|
|
| 35 |
"input": input_text,
|
| 36 |
"predictions": predictions
|
| 37 |
}
|
|
|
|
|
|
|
| 38 |
with open("log.jsonl", "a") as f:
|
| 39 |
f.write(json.dumps(log_entry, default=str) + "\n")
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
label_counter = Counter([item["entity_group"] for item in predictions])
|
| 42 |
tag_labels = list(label_counter.keys())
|
| 43 |
tag_counts = list(label_counter.values())
|
|
@@ -45,6 +54,18 @@ def index():
|
|
| 45 |
return render_template("index.html", predictions=predictions, input_text=input_text,
|
| 46 |
tag_labels=tag_labels, tag_counts=tag_counts)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
if __name__ == "__main__":
|
| 49 |
port = int(os.environ.get("PORT", 7860))
|
| 50 |
app.run(host="0.0.0.0", port=port)
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template, send_file
|
| 2 |
from transformers import AutoModelForTokenClassification, RobertaTokenizer, pipeline
|
| 3 |
from collections import Counter
|
| 4 |
import datetime, json
|
|
|
|
| 16 |
print(f"Error loading pipeline: {str(e)}")
|
| 17 |
raise
|
| 18 |
|
| 19 |
+
# Main route
|
| 20 |
@app.route("/", methods=["GET", "POST"])
|
| 21 |
def index():
|
| 22 |
predictions = []
|
|
|
|
| 36 |
"input": input_text,
|
| 37 |
"predictions": predictions
|
| 38 |
}
|
| 39 |
+
|
| 40 |
+
# Save and print log in the log tab
|
| 41 |
with open("log.jsonl", "a") as f:
|
| 42 |
f.write(json.dumps(log_entry, default=str) + "\n")
|
| 43 |
|
| 44 |
+
print("\n--- LOG FILE CONTENTS ---")
|
| 45 |
+
with open("log.jsonl", "r") as f:
|
| 46 |
+
for line in f:
|
| 47 |
+
print(line.strip())
|
| 48 |
+
print("--- END OF LOG ---\n")
|
| 49 |
+
|
| 50 |
label_counter = Counter([item["entity_group"] for item in predictions])
|
| 51 |
tag_labels = list(label_counter.keys())
|
| 52 |
tag_counts = list(label_counter.values())
|
|
|
|
| 54 |
return render_template("index.html", predictions=predictions, input_text=input_text,
|
| 55 |
tag_labels=tag_labels, tag_counts=tag_counts)
|
| 56 |
|
| 57 |
+
# Route to view log
|
| 58 |
+
@app.route("/view_log")
|
| 59 |
+
def view_log():
|
| 60 |
+
with open("log.jsonl", "r") as f:
|
| 61 |
+
contents = f.read()
|
| 62 |
+
return f"<pre>{contents}</pre>"
|
| 63 |
+
|
| 64 |
+
# Route to download the log
|
| 65 |
+
@app.route("/download_log")
|
| 66 |
+
def download_log():
|
| 67 |
+
return send_file("log.jsonl", as_attachment=True)
|
| 68 |
+
|
| 69 |
if __name__ == "__main__":
|
| 70 |
port = int(os.environ.get("PORT", 7860))
|
| 71 |
app.run(host="0.0.0.0", port=port)
|