generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt4-o.py
151 lines (120 loc) · 5.02 KB
/
gpt4-o.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from openai import OpenAI
import dotenv
import base64
import json
import os
import logging
from telegram import(
Update,
InlineKeyboardButton,
InlineKeyboardMarkup)
from telegram.ext import(
ApplicationBuilder,
CommandHandler,
MessageHandler,
filters,
ContextTypes,
CallbackQueryHandler)
dotenv.load_dotenv()
client = OpenAI(api_key = os.getenv("OPENAI_API_KEY"))
# Set up logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
prompt = """Extract information from package labels in the image and
return as a JSON object with this exact structure:
{
"product_name": {"value": "<value>", "accuracy": <value>},
"lot_number": {"value": "<value>", "accuracy": <value>},
"quantity": {"value": "<value>", "accuracy": <value>},
"expiry_date": {"value": "<value>", "accuracy": <value>},
"gtin_14": {"value": "<value>", "accuracy": <value>},
"complete_barcode": {"value": "<value>", "accuracy": <value>}
}
Include product name and expiry date as the most important. Provide accuracy for each entity.
"""
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text('Hi! Send me an image, and I will process it for you.')
async def handle_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Get the file id of the image
file_id = update.message.photo[-1].file_id
new_file = await context.bot.get_file(file_id)
# Download the image
file_path = os.path.join('downloads', f'{file_id}.jpg')
os.makedirs('downloads', exist_ok=True)
await new_file.download_to_drive(file_path)
# Process the image with GPT-4 (this example assumes OCR or some image-to-text processing)
response_text = process_image_with_gpt4(file_path)
# Create inline keyboard with a button to modify the response
keyboard = [
[InlineKeyboardButton("Modify Response", callback_data='modify_response')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
# Send the response back to the user with the button
await update.message.reply_text(response_text, reply_markup=reply_markup)
def process_image_with_gpt4(file_path: str) -> str:
# Process the image with GPT4-o
with open(file_path, 'rb') as image_file:
image_content = base64.b64encode(image_file.read()).decode("utf-8")
response = client.chat.completions.create(
model="gpt-4o",
response_format={"type": "json_object"},
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_content}"
}
}
]
},
],
temperature=0,
max_tokens=1024,
top_p=1,
frequency_penalty=0.1,
presence_penalty=0.1,
)
response_message_content = response.choices[0].message.content
response_data = json.loads(response_message_content)
product_name = response_data["product_name"]['value']
expiry_date = response_data["expiry_date"]['value']
return f"Product Name: {product_name}\nExpiry Date: {expiry_date}"
async def modify_response(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
query = update.callback_query
await query.answer()
# Send a message to the user to enter the corrected response
await query.edit_message_text(
text="Please enter the corrected response in the following format:\nProduct Name: nom du produit \nExpiry Date: dd/mm/yy"
)
# Set up a handler to capture the user's corrected response
context.user_data['awaiting_correction'] = True
async def receive_correction(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if context.user_data.get('awaiting_correction'):
corrected_response = update.message.text
await update.message.reply_text(f"Received corrected response:\n{corrected_response}")
context.user_data['awaiting_correction'] = False
def main() -> None:
# Create the Application and pass it your bot's token
application = ApplicationBuilder().token(os.environ.get("TG_TOKEN")).build()
# Add command handler for the start command
application.add_handler(CommandHandler("start", start))
# Add handler for receiving images
application.add_handler(MessageHandler(filters.PHOTO, handle_image))
# Add handler for callback queries (button presses)
application.add_handler(CallbackQueryHandler(modify_response, pattern='modify_response'))
# Add handler for receiving corrected response
application.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), receive_correction))
# Start the Bot
application.run_polling()
if __name__ == '__main__':
main()