-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelper.py
74 lines (59 loc) · 2.12 KB
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import requests
import streamlit as st
import base64
import locale
from dotenv import load_dotenv
import os
# Set the locale to the user's default locale
locale.setlocale(locale.LC_ALL, '')
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
#base_api_url = "http://localhost:7071"
def get_open_ai_response(image, mime):
with st.spinner(text="Analyzing the image..."):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
base64_image = base64.b64encode(image).decode('utf-8')
prompt = """
Extract the information related to the petition, title, description and information from the table of the people that have signed it.
Return a response strictly in this JSON format and don't include any text before or after:
{
"title": "string (if not available, empty string)",
"description: "string (if not available, empty string)",
"signatures: [
{
"name": "string",
"mobile" "number",
"email": "email",
"postcode: "string",
"contact_by_email": "bool",
"contact_by_phone": "bool"
}
]
"""
payload = {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 1200
}
response = requests.post("https://api.openai.com/v1/chat/completions",headers=headers, json=payload)
json_response = response.json()['choices'][0]['message']['content']
return json_response