zahraa12355 commited on
Commit
3dc29c2
·
verified ·
1 Parent(s): 77e6391

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ !pip install groq pypdf gradio
11
+
12
+ from groq import Groq
13
+ from pypdf import PdfReader
14
+ import gradio as gr
15
+
16
+ # ---------------------------------------
17
+ # 1. Initialize Groq Client
18
+ # ---------------------------------------
19
+ client = Groq(api_key="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")
20
+
21
+
22
+ # ---------------------------------------
23
+ # 2. Extract text from PDF
24
+ # ---------------------------------------
25
+ def extract_pdf_text(pdf_file):
26
+ reader = PdfReader(pdf_file.name)
27
+ text = ""
28
+ for page in reader.pages:
29
+ extracted = page.extract_text()
30
+ if extracted:
31
+ text += extracted + "\n"
32
+ return text
33
+
34
+
35
+ # ---------------------------------------
36
+ # 3. Ask Groq to classify + extract fields
37
+ # ---------------------------------------
38
+ def analyze_document(text):
39
+ prompt = f"""
40
+ You are an expert document analysis AI.
41
+
42
+ Given the following document text, do 3 things:
43
+
44
+ 1. Identify the document type (invoice, receipt, contract, report, certificate, etc.)
45
+ 2. Extract key fields in JSON format.
46
+ 3. Provide a short summary.
47
+
48
+ Document text:
49
+ {text}
50
+
51
+ Return your answer in this JSON structure:
52
+
53
+ {{
54
+ "document_type": "",
55
+ "fields": {{}},
56
+ "summary": ""
57
+ }}
58
+ """
59
+
60
+ response = client.chat.completions.create(
61
+ model="llama-3.1-8b-instant",
62
+ messages=[{"role": "user", "content": prompt}],
63
+ temperature=0
64
+ )
65
+
66
+ return response.choices[0].message.content