Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,77 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
-
from
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
| 11 |
-
|
| 12 |
-
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 13 |
-
|
| 14 |
-
@app.route("/")
|
| 15 |
-
def home():
|
| 16 |
-
return "✅ UniSolve (Mistral 7B) backend is running."
|
| 17 |
|
| 18 |
@app.route("/generate", methods=["POST"])
|
| 19 |
def generate():
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
return jsonify({"error": str(e)}), 500
|
| 39 |
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_file
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
from fpdf import FPDF
|
| 4 |
+
from docx import Document
|
| 5 |
+
import openai
|
| 6 |
+
import os
|
| 7 |
+
import uuid
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
+
CORS(app)
|
| 11 |
|
| 12 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
@app.route("/generate", methods=["POST"])
|
| 15 |
def generate():
|
| 16 |
+
try:
|
| 17 |
+
data = request.json
|
| 18 |
+
subject = data.get("category", "")
|
| 19 |
+
citation = data.get("citationStyle", "")
|
| 20 |
+
case_study = data.get("caseStudy", "")
|
| 21 |
+
questions = data.get("questions", "")
|
| 22 |
+
|
| 23 |
+
prompt = (
|
| 24 |
+
f"You are an academic writing assistant for university students. "
|
| 25 |
+
f"Respond in Times New Roman, using university-level detail. Use the following:\n"
|
| 26 |
+
f"- Subject: {subject}\n"
|
| 27 |
+
f"- Citation Style: {citation}\n"
|
| 28 |
+
f"- Case Study: {case_study}\n"
|
| 29 |
+
f"- Questions: {questions}\n"
|
| 30 |
+
f"Respond with a clear, structured academic answer using {citation} citation style. "
|
| 31 |
+
f"Use credible university-grade sources with proper citations. "
|
| 32 |
+
f"Do NOT add a conclusion unless asked. Only answer the questions."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
completion = openai.ChatCompletion.create(
|
| 36 |
+
model="gpt-3.5-turbo-0125",
|
| 37 |
+
messages=[{"role": "user", "content": prompt}],
|
| 38 |
+
max_tokens=3000,
|
| 39 |
+
temperature=0.7
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
result = completion.choices[0].message["content"]
|
| 43 |
+
return jsonify({"response": result})
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return jsonify({"error": str(e)}), 500
|
| 47 |
+
|
| 48 |
+
@app.route("/export/pdf", methods=["POST"])
|
| 49 |
+
def export_pdf():
|
| 50 |
+
try:
|
| 51 |
+
content = request.json.get("content", "")
|
| 52 |
+
filename = f"/tmp/{uuid.uuid4().hex}.pdf"
|
| 53 |
+
pdf = FPDF()
|
| 54 |
+
pdf.add_page()
|
| 55 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 56 |
+
pdf.set_font("Times", size=12)
|
| 57 |
+
for line in content.split("\n"):
|
| 58 |
+
pdf.multi_cell(0, 10, line)
|
| 59 |
+
pdf.output(filename)
|
| 60 |
+
return send_file(filename, as_attachment=True)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return jsonify({"error": str(e)}), 500
|
| 63 |
|
| 64 |
+
@app.route("/export/word", methods=["POST"])
|
| 65 |
+
def export_word():
|
| 66 |
try:
|
| 67 |
+
content = request.json.get("content", "")
|
| 68 |
+
filename = f"/tmp/{uuid.uuid4().hex}.docx"
|
| 69 |
+
doc = Document()
|
| 70 |
+
doc.styles["Normal"].font.name = "Times New Roman"
|
| 71 |
+
for line in content.split("\n"):
|
| 72 |
+
doc.add_paragraph(line)
|
| 73 |
+
doc.save(filename)
|
| 74 |
+
return send_file(filename, as_attachment=True)
|
| 75 |
except Exception as e:
|
| 76 |
return jsonify({"error": str(e)}), 500
|
| 77 |
|