File size: 2,784 Bytes
4596f0a
 
 
 
 
 
 
228d8eb
 
4596f0a
228d8eb
b89ce0a
228d8eb
 
 
4596f0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b89ce0a
 
4596f0a
 
 
 
 
b89ce0a
4596f0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228d8eb
4596f0a
 
228d8eb
4596f0a
 
 
 
 
 
 
 
228d8eb
 
7230f20
 
 
228d8eb
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
from fpdf import FPDF
from docx import Document
import openai
import os
import uuid

app = Flask(__name__)
CORS(app)

client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

@app.route("/generate", methods=["POST"])
def generate():
    try:
        data = request.json
        subject = data.get("category", "")
        citation = data.get("citationStyle", "")
        case_study = data.get("caseStudy", "")
        questions = data.get("questions", "")

        prompt = (
            f"You are an academic writing assistant for university students. "
            f"Respond in Times New Roman, using university-level detail. Use the following:\n"
            f"- Subject: {subject}\n"
            f"- Citation Style: {citation}\n"
            f"- Case Study: {case_study}\n"
            f"- Questions: {questions}\n"
            f"Respond with a clear, structured academic answer using {citation} citation style. "
            f"Use credible university-grade sources with proper citations. "
            f"Do NOT add a conclusion unless asked. Only answer the questions."
        )

        chat_response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": prompt}],
            max_tokens=3000,
            temperature=0.7
        )

        result = chat_response.choices[0].message.content
        return jsonify({"response": result})

    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route("/export/pdf", methods=["POST"])
def export_pdf():
    try:
        content = request.json.get("content", "")
        filename = f"/tmp/{uuid.uuid4().hex}.pdf"
        pdf = FPDF()
        pdf.add_page()
        pdf.set_auto_page_break(auto=True, margin=15)
        pdf.set_font("Times", size=12)
        for line in content.split("\n"):
            pdf.multi_cell(0, 10, line)
        pdf.output(filename)
        return send_file(filename, as_attachment=True)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route("/export/word", methods=["POST"])
def export_word():
    try:
        content = request.json.get("content", "")
        filename = f"/tmp/{uuid.uuid4().hex}.docx"
        doc = Document()
        doc.styles["Normal"].font.name = "Times New Roman"
        for line in content.split("\n"):
            doc.add_paragraph(line)
        doc.save(filename)
        return send_file(filename, as_attachment=True)
    except Exception as e:
        return jsonify({"error": str(e)}), 500
@app.route("/")
def index():
    return "✅ UniSolve backend is running. Visit via the proper frontend."
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)