tomwatto09 commited on
Commit
ff79d46
Β·
verified Β·
1 Parent(s): 98b3dcd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -108
app.py CHANGED
@@ -1,115 +1,32 @@
1
- import openai
2
- import gradio as gr
3
  import os
4
- from fpdf import FPDF
5
- import tempfile
6
 
7
- # Set your OpenAI API key
8
- openai.api_key = "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"
9
 
10
- history = []
 
11
 
12
- def generate_essay(category, case_study, questions):
13
- prompt = (
14
- f"You are EssayMate, a university-level academic writing assistant specialised in {category}. "
15
- "Your job is to help students respond to complex academic prompts with accurate, structured, and well-referenced answers.\n\n"
16
- "Your rules are:\n"
17
- "- Use a formal academic tone.\n"
18
- "- Include only reliable, university-grade sources with Harvard-style citations.\n"
19
- "- If no source can be confirmed, write: '[Citation available upon request or from verified academic databases]'.\n"
20
- "- Do not include examples unless explicitly asked.\n"
21
- "- Do not include a conclusion unless requested.\n\n"
22
- f"CASE STUDY:\n{case_study}\n\n"
23
- f"QUESTIONS:\n{questions}\n\n"
24
- "Write the response below:\n"
25
- )
26
-
27
- try:
28
- response = openai.chat.completions.create(
29
- model="gpt-3.5-turbo",
30
- messages=[
31
- {"role": "system", "content": f"You are a Harvard-style academic writing assistant in {category}."},
32
- {"role": "user", "content": prompt}
33
- ],
34
- temperature=0.7,
35
- max_tokens=1800
36
- )
37
- reply = response.choices[0].message.content.strip()
38
- history.append(reply)
39
- return reply
40
- except Exception as e:
41
- return f"⚠️ Error: {str(e)}"
42
-
43
- def download_pdf(text):
44
- pdf = FPDF()
45
- pdf.add_page()
46
- pdf.set_auto_page_break(auto=True, margin=15)
47
- pdf.set_font("Arial", size=12)
48
- for line in text.split("\n"):
49
- pdf.multi_cell(0, 10, line)
50
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
51
- pdf.output(temp_file.name)
52
- return temp_file.name
53
-
54
- # Gradio UI
55
- with gr.Blocks(css="""
56
- body {
57
- background-color: #f2f8ff;
58
- font-family: 'Inter', 'Helvetica Neue', Helvetica, sans-serif;
59
- color: #003366;
60
- }
61
- .gr-box, textarea, input, .gr-textbox textarea {
62
- background-color: #e6f0ff !important;
63
- border: 1px solid #99c2ff !important;
64
- border-radius: 10px !important;
65
- padding: 14px !important;
66
- font-size: 15px;
67
- color: #003366 !important;
68
- }
69
- .gr-button {
70
- background-color: #0066cc !important;
71
- color: white !important;
72
- border-radius: 10px !important;
73
- padding: 12px 20px !important;
74
- font-size: 16px !important;
75
- font-weight: 600;
76
- transition: background-color 0.3s ease;
77
- }
78
- .gr-button:hover {
79
- background-color: #005bb5 !important;
80
- }
81
- h1, h2, h3, .gr-markdown, .gr-markdown h1 {
82
- color: #002c5e !important;
83
- font-weight: 600;
84
- }
85
- label {
86
- font-weight: 600;
87
- color: #002855 !important;
88
- }
89
- """) as iface:
90
- gr.Markdown("# 🧠 EssayMate β€” Academic AI Writing Tool")
91
- gr.Markdown("AI-powered academic support with Harvard-style referencing. Designed for students in Nursing, Psychology, Law, Education, and more.")
92
-
93
- with gr.Row():
94
- category = gr.Dropdown(
95
- choices=["Nursing", "Psychology", "Law", "Education", "Other"],
96
- label="πŸ“‚ Subject Area",
97
- value="Nursing"
98
- )
99
 
100
- with gr.Row():
101
- with gr.Column():
102
- case_study = gr.Textbox(lines=10, label="🩺 Case Study")
103
- with gr.Column():
104
- questions = gr.Textbox(lines=20, label="❓ Questions")
105
-
106
- generate_btn = gr.Button("✍️ Generate Response")
107
- output = gr.Textbox(lines=30, label="πŸ“„ EssayMate Output")
108
-
109
- download_btn = gr.Button("πŸ“„ Download as PDF")
110
- file_output = gr.File(label="Download Link")
111
 
112
- generate_btn.click(fn=generate_essay, inputs=[category, case_study, questions], outputs=output)
113
- download_btn.click(fn=download_pdf, inputs=output, outputs=file_output)
 
 
 
 
114
 
115
- iface.launch()
 
1
+ from flask import Flask, request, jsonify
2
+ from openai import OpenAI
3
  import os
 
 
4
 
5
+ app = Flask(__name__)
 
6
 
7
+ openai_api_key = os.getenv("OPENAI_API_KEY")
8
+ client = OpenAI(api_key=openai_api_key)
9
 
10
+ @app.route("/generate", methods=["POST"])
11
+ def generate():
12
+ data = request.json
13
+ case_study = data.get("case_study", "")
14
+ questions = data.get("questions", "")
15
+ subject = data.get("category", "")
16
+ style = data.get("citation", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ full_prompt = (
19
+ f"You are an academic assistant for {subject}.\n"
20
+ f"Use {style} citation style. Write clear, well-structured university-level responses.\n\n"
21
+ f"Case Study:\n{case_study}\n\n"
22
+ f"Questions:\n{questions}\n"
23
+ )
 
 
 
 
 
24
 
25
+ response = client.chat.completions.create(
26
+ model="gpt-3.5-turbo",
27
+ messages=[{"role": "user", "content": full_prompt}],
28
+ temperature=0.5,
29
+ max_tokens=2048
30
+ )
31
 
32
+ return jsonify({"answer": response.choices[0].message.content.strip()})