Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,17 @@ from fastapi import FastAPI, UploadFile, File
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from groq import Groq
|
| 4 |
from pypdf import PdfReader
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
# Initialize Groq client
|
| 9 |
-
client = Groq(api_key="
|
| 10 |
|
| 11 |
-
# -----------------------------
|
| 12 |
# Extract text from PDF
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
reader = PdfReader(pdf_file)
|
| 16 |
text = ""
|
| 17 |
for page in reader.pages:
|
| 18 |
extracted = page.extract_text()
|
|
@@ -20,9 +20,7 @@ def extract_pdf_text(pdf_file):
|
|
| 20 |
text += extracted + "\n"
|
| 21 |
return text
|
| 22 |
|
| 23 |
-
# -----------------------------
|
| 24 |
# Analyze text with Groq
|
| 25 |
-
# -----------------------------
|
| 26 |
def analyze_text_with_groq(text):
|
| 27 |
prompt = f"""
|
| 28 |
You are an expert document analysis AI.
|
|
@@ -50,16 +48,13 @@ Return your answer in this JSON structure:
|
|
| 50 |
|
| 51 |
return response.choices[0].message.content
|
| 52 |
|
| 53 |
-
# -----------------------------
|
| 54 |
-
# FastAPI Endpoint
|
| 55 |
-
# -----------------------------
|
| 56 |
@app.post("/analyze")
|
| 57 |
async def analyze_document(file: UploadFile = File(...)):
|
| 58 |
-
# Extract text from uploaded PDF
|
| 59 |
pdf_bytes = await file.read()
|
| 60 |
text = extract_pdf_text(pdf_bytes)
|
| 61 |
-
|
| 62 |
-
# Analyze with Groq
|
| 63 |
result = analyze_text_with_groq(text)
|
|
|
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from groq import Groq
|
| 4 |
from pypdf import PdfReader
|
| 5 |
+
import io
|
| 6 |
+
import uvicorn
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
# Initialize Groq client
|
| 11 |
+
client = Groq(api_key="YOUR_API_KEY_HERE")
|
| 12 |
|
|
|
|
| 13 |
# Extract text from PDF
|
| 14 |
+
def extract_pdf_text(pdf_bytes):
|
| 15 |
+
reader = PdfReader(io.BytesIO(pdf_bytes))
|
|
|
|
| 16 |
text = ""
|
| 17 |
for page in reader.pages:
|
| 18 |
extracted = page.extract_text()
|
|
|
|
| 20 |
text += extracted + "\n"
|
| 21 |
return text
|
| 22 |
|
|
|
|
| 23 |
# Analyze text with Groq
|
|
|
|
| 24 |
def analyze_text_with_groq(text):
|
| 25 |
prompt = f"""
|
| 26 |
You are an expert document analysis AI.
|
|
|
|
| 48 |
|
| 49 |
return response.choices[0].message.content
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
@app.post("/analyze")
|
| 52 |
async def analyze_document(file: UploadFile = File(...)):
|
|
|
|
| 53 |
pdf_bytes = await file.read()
|
| 54 |
text = extract_pdf_text(pdf_bytes)
|
|
|
|
|
|
|
| 55 |
result = analyze_text_with_groq(text)
|
| 56 |
+
return {"result": result}
|
| 57 |
|
| 58 |
+
# REQUIRED for Hugging Face Docker Spaces
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|