tstech1 commited on
Commit
bdf5dbd
Β·
verified Β·
1 Parent(s): ea7729b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +21 -21
main.py CHANGED
@@ -6,7 +6,7 @@ from openai import OpenAI
6
 
7
  app = FastAPI()
8
 
9
- # βœ… Initialize OpenAI client (use environment variable for safety)
10
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
11
 
12
  # πŸ“– Extract text from PDF
@@ -21,39 +21,39 @@ def extract_pdf_text(file) -> str:
21
 
22
 
23
  @app.post("/Query-pdf")
24
- async def generate_questions(query: str = Form(...), pdf: UploadFile = File(...)):
25
  try:
26
- # Read uploaded PDF
27
  pdf_text = extract_pdf_text(pdf.file)
28
 
29
- # πŸ”₯ Call OpenAI GPT-4o-mini model
 
 
 
 
 
 
 
 
 
30
  chat_completion = client.chat.completions.create(
31
  model="gpt-4o-mini",
32
  messages=[
33
- {
34
- "role": "system",
35
- "content": (
36
- "You are a helpful assistant that answers questions based strictly "
37
- "on the provided PDF content. If the answer cannot be found, say so."
38
- )
39
- },
40
  {
41
  "role": "user",
42
- "content": f"""
43
- Here is the PDF content (may be truncated):
44
- {pdf_text[:15000]}
45
-
46
- Now, answer this query: {query}
47
- """
48
  }
49
  ],
50
- temperature=0.3,
51
- max_tokens=1200
52
  )
53
 
 
 
54
  return JSONResponse({
55
  "success": True,
56
- "answer": chat_completion.choices[0].message.content
57
  })
58
 
59
  except Exception as e:
@@ -62,4 +62,4 @@ Now, answer this query: {query}
62
 
63
  @app.get("/")
64
  def home():
65
- return {"message": "βœ… PDF Query API is running!"}
 
6
 
7
  app = FastAPI()
8
 
9
+ # βœ… Initialize OpenAI client
10
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
11
 
12
  # πŸ“– Extract text from PDF
 
21
 
22
 
23
  @app.post("/Query-pdf")
24
+ async def query_pdf(query: str = Form(...), pdf: UploadFile = File(...)):
25
  try:
26
+ # Extract text from PDF
27
  pdf_text = extract_pdf_text(pdf.file)
28
 
29
+ # Restrictive system instruction
30
+ system_prompt = (
31
+ "You are a strict PDF-based question-answering assistant. "
32
+ "You must answer ONLY using the text from the provided PDF. "
33
+ "If the answer cannot be found explicitly or clearly inferred "
34
+ "from the PDF, respond strictly with: 'The answer is not available in the PDF.' "
35
+ "Do not use outside knowledge or assumptions."
36
+ )
37
+
38
+ # Call OpenAI GPT model
39
  chat_completion = client.chat.completions.create(
40
  model="gpt-4o-mini",
41
  messages=[
42
+ {"role": "system", "content": system_prompt},
 
 
 
 
 
 
43
  {
44
  "role": "user",
45
+ "content": f"PDF Content:\n{pdf_text[:15000]}\n\nQuery: {query}"
 
 
 
 
 
46
  }
47
  ],
48
+ temperature=0.0, # ⚑ ensures deterministic factual answers
49
+ max_tokens=1000
50
  )
51
 
52
+ answer = chat_completion.choices[0].message.content.strip()
53
+
54
  return JSONResponse({
55
  "success": True,
56
+ "answer": answer
57
  })
58
 
59
  except Exception as e:
 
62
 
63
  @app.get("/")
64
  def home():
65
+ return {"message": "βœ… Strict PDF Query API is running!"}