-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
115 lines (97 loc) · 3.95 KB
/
main.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
# /usr/bin/nuhman/bughunter0
from dotenv import load_dotenv
from pyrogram import filters, Client
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message
from stickers import stickers
import google.generativeai as genai
import os
import PIL.Image
import random
import time
load_dotenv()
API_HASH = os.getenv("API_HASH")
API_ID = os.getenv("API_ID")
BOT_TOKEN = os.getenv("BOT_TOKEN")
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
GITHUB_BUTTON = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
"Source Code", url="https://github.com/nuhmanpk/VisionScriptBot"
)
]
]
)
app = Client(
"VisionScriptBot", api_hash=API_HASH, api_id=int(API_ID), bot_token=BOT_TOKEN
)
genai.configure(api_key=GOOGLE_API_KEY)
@app.on_message(filters.command("start") & filters.private)
async def start(_, message: Message):
welcome_message = (
f"👋 Hey @{message.chat.username}!\n\n"
"I'm here to help. Just send me an image, and I'll do the rest.\n\n"
"Feel free to explore and use my features. If you have any questions or need assistance, "
"you can use the /help command.\n\n"
"🤖 Don't forget to join @BughunterBots for more awesome bots like me!\n"
)
await message.reply(welcome_message, quote=True)
@app.on_message(filters.command("help") & filters.private)
async def help_command(_, message: Message):
help_message = (
"🤖 **How to use the Transcription Bot**\n\n"
"1. **Send an Image:** Simply send me an image containing text that you want transcribed.\nGot any question regarding the image? add it in the image caption before uploading.\n"
"2. **Wait for Transcription:** I'll process the image and provide you with the transcribed text.\n\n"
"For updates and more bots, join @BughunterBots 🚀\n"
)
await message.reply(help_message, quote=True)
@app.on_message(filters.photo & filters.private)
async def vision(bot, message: Message):
try:
model_name = "gemini-1.5-pro"
sticker_id = random.choice(stickers)
sticker = await message.reply_sticker(sticker_id)
txt = await message.reply(f"Loading {model_name} ...")
model = genai.GenerativeModel(model_name)
await txt.edit("Downloading Photo ....")
file_path = await message.download()
caption = message.caption
img = PIL.Image.open(file_path)
await txt.edit("Shhh 🤫 , Gemini Vision Pro is At Work ⚠️.\n Pls Wait..")
response = (
model.generate_content([caption, img])
if caption
else model.generate_content(img)
)
os.remove(file_path)
await txt.edit('@VisionScriptBot is cooking...')
await sticker.delete()
await txt.delete()
if response.parts: # handle multiline resps
for part in response.parts:
await message.reply(part.text, reply_markup=GITHUB_BUTTON)
time.sleep(2)
elif response.text:
await message.reply(response.text, reply_markup=GITHUB_BUTTON)
else:
await message.reply(
"Couldn't figure out what's in the Image. Contact @bughunter0 for help."
)
except Exception as e:
await message.reply("Something Bad occured, Contact @bughunter0")
raise e
@app.on_message(filters.document & filters.private)
async def document(bot, message: Message):
await message.reply(
"Documents are not supported, Please the File as Image !!!\n\n @BughunterBots"
)
@app.on_message(filters.command("source") & filters.private)
async def source(bot, message: Message):
msg = (
"Here is the source code for the bot 🚀\n\n"
"Follow me for updates , and add Your star if you find this helpful 🌟\n\n"
"Thank you for your support! 👏\n\n"
"Happy Coding! 🚀"
)
await message.reply(msg, reply_markup=GITHUB_BUTTON)
app.run(print("Bot Started..."))