kaiozwald commited on
Commit
05e0e3b
·
1 Parent(s): df96c74

Initial clean commit without token

Browse files
Files changed (3) hide show
  1. Dockerfile +13 -0
  2. app.py +113 -0
  3. requirement.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ WORKDIR /app
8
+
9
+ COPY --chown=user ./requirements.txt requirements.txt
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ COPY --chown=user . /app
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # !pip install fastapi uvicorn huggingface_hub python-docx --quiet
2
+
3
+ from fastapi import FastAPI, UploadFile, File
4
+ from fastapi.responses import FileResponse
5
+ from pydantic import BaseModel
6
+ from typing import List
7
+ from huggingface_hub import InferenceClient
8
+ from docx import Document
9
+ from docx.shared import Pt
10
+ from docx.enum.text import WD_ALIGN_PARAGRAPH
11
+ from datetime import datetime
12
+ from collections import defaultdict
13
+ import uuid
14
+ import os
15
+ app = FastAPI()
16
+
17
+ client = InferenceClient(
18
+ model="meta-llama/Meta-Llama-3-8B-Instruct",
19
+ token=os.environ["HF_TOKEN"]
20
+ )
21
+
22
+ SECTIONS = [
23
+ "Abstract",
24
+ "Introduction",
25
+ "Literature Review",
26
+ "Methodology",
27
+ "System Design",
28
+ "Implementation",
29
+ "Evaluation",
30
+ "Conclusion and Future Work"
31
+ ]
32
+
33
+
34
+ class TaskItem(BaseModel):
35
+ task: str
36
+
37
+ class ReportRequest(BaseModel):
38
+ tasks: List[TaskItem]
39
+
40
+ def build_overview(self) -> str:
41
+ overview = ["This project summarizes a year of academic effort including planning, development, and evaluation.\n"]
42
+ for item in self.tasks:
43
+ overview.append(f"- {item.task}")
44
+ overview.append("")
45
+ return "\n".join(overview)
46
+
47
+ def generate_section(section_name: str, overview: str) -> str:
48
+ prompt = f"""
49
+ You are a professional academic writer. Write a detailed section titled '{section_name}' for a Computer Science graduation project report based on the following project overview:
50
+
51
+ \"\"\"{overview}\"\"\"
52
+
53
+ Guidelines:
54
+ - Do NOT include any 'References' at the end of this section.
55
+ - Do NOT include any 'Conclusion' at the end of this section.
56
+ - ONLY write diagrams or tables if they are clearly described in the overview or explicitly required by the user.
57
+ - Use a professional, academic tone with deep elaboration and well-structured content.
58
+ - Do not repeat content across sections.
59
+ - Do not generate a table of contents here.
60
+ - Aim for long and rich content (2 to 10+ pages per section).
61
+ """
62
+ response = client.chat_completion(
63
+ messages=[
64
+ {"role": "system", "content": "You are a helpful assistant that writes academic reports."},
65
+ {"role": "user", "content": prompt}
66
+ ],
67
+ max_tokens=4096,
68
+ temperature=0.7
69
+ )
70
+ return response.choices[0].message.content.strip()
71
+
72
+ def create_word_doc(sections_content, filename="Graduation_Project_Report.docx"):
73
+ doc = Document()
74
+ style = doc.styles['Normal']
75
+ font = style.font
76
+ font.name = 'Times New Roman'
77
+ font.size = Pt(12)
78
+
79
+ doc.add_heading("Graduation Project Report", 0).alignment = WD_ALIGN_PARAGRAPH.CENTER
80
+ doc.add_paragraph("Author: Your Name")
81
+ doc.add_paragraph("Supervisor: Supervisor Name")
82
+ doc.add_paragraph("Department: Computer Science")
83
+ doc.add_paragraph("University: Your University")
84
+ doc.add_paragraph("Date: " + datetime.today().strftime("%B %d, %Y"))
85
+ doc.add_page_break()
86
+
87
+ for section_name, section_text in sections_content.items():
88
+ doc.add_heading(section_name, level=1)
89
+ for paragraph in section_text.split('\n'):
90
+ line = paragraph.strip()
91
+ if line:
92
+ doc.add_paragraph(line)
93
+ doc.add_page_break()
94
+
95
+ doc.save(filename)
96
+ return filename
97
+
98
+ @app.post("/generate-report")
99
+ def generate_report(req: ReportRequest):
100
+ overview = req.build_overview()
101
+
102
+ sections = {}
103
+ for section in SECTIONS:
104
+ sections[section] = generate_section(section, overview)
105
+
106
+ unique_filename = f"report_{uuid.uuid4().hex[:8]}.docx"
107
+ create_word_doc(sections, unique_filename)
108
+
109
+ return {"download_url": f"/download/{unique_filename}"}
110
+
111
+ @app.get("/download/{filename}")
112
+ def download_file(filename: str):
113
+ return FileResponse(path=filename, filename=filename, media_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
requirement.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ huggingface_hub
4
+ python-docx