kaiozwald commited on
Commit
e58a657
·
1 Parent(s): a20234d

Switched to free model distilgpt2

Browse files
Files changed (3) hide show
  1. app.py +13 -32
  2. requirements.txt +2 -1
  3. test.py +15 -0
app.py CHANGED
@@ -1,23 +1,18 @@
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",
@@ -30,7 +25,6 @@ SECTIONS = [
30
  "Conclusion and Future Work"
31
  ]
32
 
33
-
34
  class TaskItem(BaseModel):
35
  task: str
36
 
@@ -46,28 +40,14 @@ class ReportRequest(BaseModel):
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()
@@ -111,6 +91,7 @@ def generate_report(req: ReportRequest):
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')
 
114
  @app.get("/")
115
  def root():
116
  return {"status": "Running"}
 
1
+ from fastapi import FastAPI
 
 
2
  from fastapi.responses import FileResponse
3
  from pydantic import BaseModel
4
  from typing import List
5
+ from transformers import pipeline
6
  from docx import Document
7
  from docx.shared import Pt
8
  from docx.enum.text import WD_ALIGN_PARAGRAPH
9
  from datetime import datetime
 
10
  import uuid
11
+
12
  app = FastAPI()
13
 
14
+ # Load model once at startup
15
+ generator = pipeline("text-generation", model="distilgpt2")
 
 
16
 
17
  SECTIONS = [
18
  "Abstract",
 
25
  "Conclusion and Future Work"
26
  ]
27
 
 
28
  class TaskItem(BaseModel):
29
  task: str
30
 
 
40
 
41
  def generate_section(section_name: str, overview: str) -> str:
42
  prompt = f"""
43
+ You are an academic writer. Write a section titled '{section_name}' for a graduation project report based on:
44
+
45
+ {overview}
46
+
47
+ Avoid conclusions or references. Use formal language.
 
 
 
 
 
 
 
48
  """
49
+ result = generator(prompt, max_length=512, num_return_sequences=1)
50
+ return result[0]['generated_text']
 
 
 
 
 
 
 
51
 
52
  def create_word_doc(sections_content, filename="Graduation_Project_Report.docx"):
53
  doc = Document()
 
91
  @app.get("/download/{filename}")
92
  def download_file(filename: str):
93
  return FileResponse(path=filename, filename=filename, media_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
94
+
95
  @app.get("/")
96
  def root():
97
  return {"status": "Running"}
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  fastapi
2
  uvicorn[standard]
3
- huggingface_hub
4
  python-docx
 
 
 
1
  fastapi
2
  uvicorn[standard]
 
3
  python-docx
4
+ transformers
5
+ torch
test.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ response = requests.post(
4
+ "https://kaiozwald-BridgeIT.hf.space/generate-report",
5
+ json={
6
+ "tasks": [
7
+ {"task": "Build frontend using React"},
8
+ {"task": "Connect backend using FastAPI"},
9
+ {"task": "Use transformers to generate report sections"}
10
+ ]
11
+ }
12
+ )
13
+
14
+ print("Status Code:", response.status_code)
15
+ print("Response Text:", response.text)