Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -6,7 +6,7 @@ from openai import OpenAI
|
|
| 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,39 +21,39 @@ def extract_pdf_text(file) -> str:
|
|
| 21 |
|
| 22 |
|
| 23 |
@app.post("/Query-pdf")
|
| 24 |
-
async def
|
| 25 |
try:
|
| 26 |
-
#
|
| 27 |
pdf_text = extract_pdf_text(pdf.file)
|
| 28 |
|
| 29 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 51 |
-
max_tokens=
|
| 52 |
)
|
| 53 |
|
|
|
|
|
|
|
| 54 |
return JSONResponse({
|
| 55 |
"success": True,
|
| 56 |
-
"answer":
|
| 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!"}
|