Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,72 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def greet(name):
|
| 4 |
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import asyncio
|
| 4 |
+
from aiogram import Bot, Dispatcher
|
| 5 |
+
from aiogram import Router
|
| 6 |
+
from aiogram.fsm.storage.memory import MemoryStorage
|
| 7 |
+
from aiogram.types import Message
|
| 8 |
+
from aiogram.filters.command import Command
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
from aiogram.fsm.context import FSMContext
|
| 11 |
+
from aiogram.fsm.state import State, StatesGroup
|
| 12 |
+
import random
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
load_dotenv()
|
| 16 |
+
bot = Bot(token=os.getenv('BOT_TOKEN'))
|
| 17 |
+
dp = Dispatcher(storage=MemoryStorage())
|
| 18 |
+
bot_router = Router()
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class Step(StatesGroup):
|
| 22 |
+
guesses = State()
|
| 23 |
+
finish = State()
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@dp.message(Command("start"))
|
| 27 |
+
async def cmd_start(message: Message, state: FSMContext):
|
| 28 |
+
await message.answer("Начнём игру. Я загадал число от 1 до 100 (1 тоже может быть, и 100 тоже может быть). Число целое. Попробуй угадать какое за наименьшее количество попыток.")
|
| 29 |
+
await state.set_state(Step.guesses)
|
| 30 |
+
the_number = random.randint(1, 100)
|
| 31 |
+
await state.update_data(the_number=the_number)
|
| 32 |
+
await state.update_data(attempt=0)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@dp.message(Step.guesses)
|
| 36 |
+
async def ask(message: Message, state: FSMContext):
|
| 37 |
+
storage_data = await state.get_data()
|
| 38 |
+
attempt = int(storage_data.get('attempt', 0))
|
| 39 |
+
the_number = int(storage_data.get('the_number'))
|
| 40 |
+
attempt = attempt + 1
|
| 41 |
+
|
| 42 |
+
await state.update_data(attempt=attempt)
|
| 43 |
+
try:
|
| 44 |
+
a_number = int(message.text)
|
| 45 |
+
if a_number == the_number:
|
| 46 |
+
await message.answer('Молодец, ты отгадал!')
|
| 47 |
+
await message.answer(f'За {attempt} попыток')
|
| 48 |
+
else:
|
| 49 |
+
# не отгадал. дадим подсказку
|
| 50 |
+
if the_number > a_number:
|
| 51 |
+
await message.answer('Загаданное число больше')
|
| 52 |
+
else:
|
| 53 |
+
await message.answer('Загаданное число меньше')
|
| 54 |
+
except Exception as e:
|
| 55 |
+
await message.answer('Это не число')
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
async def main():
|
| 59 |
+
await dp.start_polling(bot)
|
| 60 |
+
await demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 61 |
+
await demo.launch()
|
| 62 |
|
| 63 |
def greet(name):
|
| 64 |
return "Hello " + name + "!!"
|
| 65 |
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
|
| 68 |
+
asyncio.run(main())
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|