-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision-gpt-telegram.py
63 lines (51 loc) · 2.19 KB
/
vision-gpt-telegram.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
from telegram import Update
from telegram.ext import Application, MessageHandler, filters, CallbackContext
from openai import OpenAI
import httpx
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
# Access environment variables
TELEGRAM_API_KEY = os.getenv("TELEGRAM_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
# Initialize OpenAI client with your API key
client = OpenAI(api_key=OPENAI_API_KEY)
async def photo_handler(update: Update, context: CallbackContext):
print("Received a photo")
photo_file = await update.message.photo[-1].get_file()
file_url = photo_file.file_path
print("File URL:", file_url) # Print the file URL
# Use httpx to asynchronously download the photo
async with httpx.AsyncClient() as client_http:
response_image = await client_http.get(file_url)
if response_image.status_code == 200:
# Save the received image temporarily
with open('photo.jpg', 'wb') as f:
f.write(response_image.content)
# Use GPT-4 Vision to describe the image
response_gpt = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{"type": "image url",
"image_url": file_url}
],
}
],
max_tokens=300,
)
print("GPT-4 Vision response:", response_gpt) # Print GPT-4 Vision response
# Extract description text from GPT-4 Vision response
description = response_gpt.choices[0].message.content
# Reply to the user with the image description
await update.message.reply_text(description)
else:
print("Failed to download image.")
if __name__ == '__main__':
application = Application.builder().token(TELEGRAM_API_KEY).build()
application.add_handler(MessageHandler(filters.PHOTO, photo_handler))
application.run_polling()