zahraa12355 commited on
Commit
d713fa3
·
verified ·
1 Parent(s): b0e78fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -39
app.py CHANGED
@@ -1,41 +1,18 @@
1
- # -*- coding: utf-8 -*-
2
- """API prog
3
-
4
- Automatically generated by Colab.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/1dwnXx2hUYegX54dlWrXEF8uF_Jj7QxX5
8
- """
9
-
10
- from fastapi import FastAPI
11
  from pydantic import BaseModel
12
  from groq import Groq
 
13
 
14
  app = FastAPI()
15
 
16
- class DocInput(BaseModel):
17
- text: str
18
-
19
- client = Groq(api_key="YOUR_KEY")
20
-
21
- @app.post("/analyze")
22
- def analyze_document(data: DocInput):
23
- return {"result": "ok"}
24
- from groq import Groq
25
- from pypdf import PdfReader
26
- import gradio as gr
27
-
28
- # ---------------------------------------
29
- # 1. Initialize Groq Client
30
- # ---------------------------------------
31
  client = Groq(api_key="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")
32
 
33
-
34
- # ---------------------------------------
35
- # 2. Extract text from PDF
36
- # ---------------------------------------
37
  def extract_pdf_text(pdf_file):
38
- reader = PdfReader(pdf_file.name)
39
  text = ""
40
  for page in reader.pages:
41
  extracted = page.extract_text()
@@ -43,16 +20,13 @@ def extract_pdf_text(pdf_file):
43
  text += extracted + "\n"
44
  return text
45
 
46
-
47
- # ---------------------------------------
48
- # 3. Ask Groq to classify + extract fields
49
- # ---------------------------------------
50
- def analyze_document(text):
51
  prompt = f"""
52
  You are an expert document analysis AI.
53
-
54
  Given the following document text, do 3 things:
55
-
56
  1. Identify the document type (invoice, receipt, contract, report, certificate, etc.)
57
  2. Extract key fields in JSON format.
58
  3. Provide a short summary.
@@ -61,7 +35,6 @@ Document text:
61
  {text}
62
 
63
  Return your answer in this JSON structure:
64
-
65
  {{
66
  "document_type": "",
67
  "fields": {{}},
@@ -75,4 +48,18 @@ Return your answer in this JSON structure:
75
  temperature=0
76
  )
77
 
78
- return response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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="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")
10
 
11
+ # -----------------------------
12
+ # Extract text from PDF
13
+ # -----------------------------
 
14
  def extract_pdf_text(pdf_file):
15
+ reader = PdfReader(pdf_file)
16
  text = ""
17
  for page in reader.pages:
18
  extracted = page.extract_text()
 
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.
 
29
  Given the following document text, do 3 things:
 
30
  1. Identify the document type (invoice, receipt, contract, report, certificate, etc.)
31
  2. Extract key fields in JSON format.
32
  3. Provide a short summary.
 
35
  {text}
36
 
37
  Return your answer in this JSON structure:
 
38
  {{
39
  "document_type": "",
40
  "fields": {{}},
 
48
  temperature=0
49
  )
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
+ return {"result": result}