Update app.py
Browse files
app.py
CHANGED
|
@@ -8,15 +8,17 @@ import base64
|
|
| 8 |
import io
|
| 9 |
import openpyxl
|
| 10 |
from datetime import datetime
|
|
|
|
| 11 |
|
| 12 |
# Load environment variables from .env file
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
def encode_image_to_base64(image_path):
|
| 22 |
"""Convert image to base64 string"""
|
|
@@ -47,12 +49,6 @@ def extract_invoice_details(image):
|
|
| 47 |
"Price/Rate": "40.00", # Numeric only
|
| 48 |
"Quantity": "2", # Numeric only
|
| 49 |
"Amount": "80.00" # Numeric only
|
| 50 |
-
},
|
| 51 |
-
{
|
| 52 |
-
"Item Name": "Product 2",
|
| 53 |
-
"Price/Rate": "19.00",
|
| 54 |
-
"Quantity": "6",
|
| 55 |
-
"Amount": "114.00"
|
| 56 |
}
|
| 57 |
],
|
| 58 |
"Total Invoice Value": "2555.00" # Numeric only, total amount
|
|
@@ -61,28 +57,32 @@ def extract_invoice_details(image):
|
|
| 61 |
Provide ONLY the dictionary, no additional text or formatting."""
|
| 62 |
|
| 63 |
# Make API call to Groq
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
}
|
| 80 |
-
]
|
| 81 |
-
}
|
| 82 |
-
]
|
| 83 |
)
|
| 84 |
-
|
| 85 |
-
return
|
| 86 |
|
| 87 |
def parse_response(response_text):
|
| 88 |
"""Parse the model's response into structured data"""
|
|
@@ -191,4 +191,4 @@ iface = gr.Interface(
|
|
| 191 |
|
| 192 |
# Launch the application
|
| 193 |
if __name__ == "__main__":
|
| 194 |
-
iface.launch()
|
|
|
|
| 8 |
import io
|
| 9 |
import openpyxl
|
| 10 |
from datetime import datetime
|
| 11 |
+
import httpx
|
| 12 |
|
| 13 |
# Load environment variables from .env file
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
+
def create_groq_client():
|
| 17 |
+
api_key = os.environ.get("GROQ_API_KEY", "")
|
| 18 |
+
return httpx.Client(
|
| 19 |
+
base_url="https://api.groq.com/openai/v1",
|
| 20 |
+
headers={"Authorization": f"Bearer {api_key}"}
|
| 21 |
+
)
|
| 22 |
|
| 23 |
def encode_image_to_base64(image_path):
|
| 24 |
"""Convert image to base64 string"""
|
|
|
|
| 49 |
"Price/Rate": "40.00", # Numeric only
|
| 50 |
"Quantity": "2", # Numeric only
|
| 51 |
"Amount": "80.00" # Numeric only
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
],
|
| 54 |
"Total Invoice Value": "2555.00" # Numeric only, total amount
|
|
|
|
| 57 |
Provide ONLY the dictionary, no additional text or formatting."""
|
| 58 |
|
| 59 |
# Make API call to Groq
|
| 60 |
+
client = create_groq_client()
|
| 61 |
+
response = client.post(
|
| 62 |
+
"/chat/completions",
|
| 63 |
+
json={
|
| 64 |
+
"model": "llama-3.2-90b-vision-preview",
|
| 65 |
+
"messages": [
|
| 66 |
+
{
|
| 67 |
+
"role": "user",
|
| 68 |
+
"content": [
|
| 69 |
+
{
|
| 70 |
+
"type": "image_url",
|
| 71 |
+
"image_url": {
|
| 72 |
+
"url": f"data:image/png;base64,{base64_image}"
|
| 73 |
+
}
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"type": "text",
|
| 77 |
+
"text": prompt
|
| 78 |
}
|
| 79 |
+
]
|
| 80 |
+
}
|
| 81 |
+
]
|
| 82 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
+
response_data = response.json()
|
| 85 |
+
return response_data['choices'][0]['message']['content']
|
| 86 |
|
| 87 |
def parse_response(response_text):
|
| 88 |
"""Parse the model's response into structured data"""
|
|
|
|
| 191 |
|
| 192 |
# Launch the application
|
| 193 |
if __name__ == "__main__":
|
| 194 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|