gabrielchua commited on
Commit
36dd136
1 Parent(s): fc2e110

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ app.py
3
+ """
4
+
5
+ import base64
6
+
7
+ import gradio as gr
8
+ from groq import Groq
9
+
10
+ # Initialize Groq client
11
+ client = Groq()
12
+
13
+ # Function to encode the image in base64
14
+ def encode_image_to_base64(image):
15
+ # Convert PIL image to base64 string
16
+ buffered = BytesIO()
17
+ image.save(buffered, format="JPEG")
18
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
19
+
20
+ # Function to process the uploaded image and extract receipt information
21
+ def extract_receipt_info(image):
22
+ # Encode the image to base64
23
+ base64_image = encode_image_to_base64(image)
24
+ `
25
+ # Send request to Groq API
26
+ chat_completion = client.chat.completions.create(
27
+ model="llama-3.2-11b-vision-preview",
28
+ messages=[
29
+ {
30
+ "role": "user",
31
+ "content": [
32
+ {
33
+ "type": "text",
34
+ "text": "Your task is to extract key information from the provided receipt image.\n\nReply in table.\n\nThis is the schema:\n- item (str), description of the item\n- price (float), price of the item\n- quantity (int), quantity of the item\n- total (float), total cost for the item"
35
+ },
36
+ {
37
+ "type": "image_url",
38
+ "image_url": {
39
+ "url": f"data:image/jpeg;base64,{base64_image}",
40
+ },
41
+ },
42
+ ],
43
+ },
44
+ {
45
+ "role": "assistant",
46
+ "content": "```markdown"
47
+ }
48
+ ],
49
+ temperature=0.1,
50
+ max_tokens=8192,
51
+ top_p=1,
52
+ stop="```",
53
+ )
54
+
55
+ # Return the response from the model
56
+ return chat_completion.choices[0].message.content
57
+
58
+ # Create the Gradio app
59
+ def gradio_app():
60
+ # Gradio interface
61
+ gr.Interface(
62
+ fn=extract_receipt_info,
63
+ inputs=gr.inputs.Image(type="pil", label="Upload Receipt Image"),
64
+ outputs="text",
65
+ title="Receipt Information Extractor",
66
+ description="Upload a receipt image and the model will extract the items, quantities, and prices from the receipt."
67
+ ).launch()
68
+
69
+ # Start the Gradio app
70
+ if __name__ == "__main__":
71
+ gradio_app()