-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
96 lines (66 loc) · 3.07 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
import asyncio
from aiogram import Bot, Dispatcher, types
from aiogram.filters import Command
from aiogram.fsm.context import FSMContext
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.fsm.state import StatesGroup, State
from aiogram import F
import google.generativeai as gemini
import os
import tempfile
API_TOKEN = ""
GEMINI_API_KEY = ""
bot = Bot(token=API_TOKEN)
storage = MemoryStorage()
dp = Dispatcher(storage=storage)
gemini.configure(api_key=GEMINI_API_KEY)
class PDFStates(StatesGroup):
waiting_for_pdf = State()
pdf_uploaded = State()
async def upload_pdf_to_gemini(file_bytes: bytes, file_name: str):
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
tmp_file.write(file_bytes)
tmp_file_path = tmp_file.name
sample_file = gemini.upload_file(path=tmp_file_path, display_name=file_name)
file = gemini.get_file(name=sample_file.name)
print(f"Retrieved file '{file.display_name}' as: {sample_file.uri}")
os.remove(tmp_file_path)
return sample_file
async def summarize_pdf_with_gemini(sample_file) -> str:
model = gemini.GenerativeModel(model_name="gemini-1.5-flash")
response = model.generate_content([sample_file, "Выполни суммаризацию этого документа. Выдели ключевые моменты по пунктам."])
return response.text
async def ask_gemini_about_pdf(sample_file, question: str) -> str:
model = gemini.GenerativeModel(model_name="gemini-1.5-flash")
response = model.generate_content([sample_file, question])
return response.text
@dp.message(Command("start"))
async def start(message: types.Message, state: FSMContext):
await message.answer(
"Отправь мне PDF документ, и я суммаризирую его, а после смогу ответить на твои вопросы о нем.")
await state.set_state(PDFStates.waiting_for_pdf)
@dp.message(F.document.mime_type == 'application/pdf')
async def handle_pdf_document(message: types.Message, state: FSMContext):
document = message.document
file = await bot.download(document)
file_bytes = file.read()
sample_file = await upload_pdf_to_gemini(file_bytes, document.file_name)
await state.update_data(sample_file=sample_file)
summary = await summarize_pdf_with_gemini(sample_file)
await message.answer(summary)
await message.answer("Теперь вы можете спросить у меня что-нибудь о документе.")
await state.set_state(PDFStates.pdf_uploaded)
@dp.message(F.text)
async def handle_questions(message: types.Message, state: FSMContext):
data = await state.get_data()
sample_file = data.get('sample_file')
if not sample_file:
await message.answer("Пожалуйста отправьте PDF документ.")
return
question = message.text
answer = await ask_gemini_about_pdf(sample_file, question)
await message.answer(answer)
async def main():
await dp.start_polling(bot)
if __name__ == '__main__':
asyncio.run(main())