Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,115 +1,32 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
-
from fpdf import FPDF
|
| 5 |
-
import tempfile
|
| 6 |
|
| 7 |
-
|
| 8 |
-
openai.api_key = "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"
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 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 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 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 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
|
|
|
| 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()})
|