Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,106 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import requests
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
+
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
| 6 |
+
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, CallbackQueryHandler, filters, ContextTypes
|
| 7 |
+
from together import Together # تأكد من تثبيت مكتبة together
|
| 8 |
+
import base64
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
from PIL import Image
|
| 11 |
|
| 12 |
+
# تحميل المتغيرات من ملف .env
|
| 13 |
+
load_dotenv()
|
| 14 |
|
| 15 |
+
# الحصول على مفتاح API من المتغيرات البيئية
|
| 16 |
+
HUGGING_FACE_API_KEY = os.getenv('HUGGING_FACE_API_KEY')
|
| 17 |
+
HUGGING_FACE_API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3.5-large-turbo"
|
| 18 |
+
HUGGING_FACE_HEADERS = {"Authorization": f"Bearer {HUGGING_FACE_API_KEY}"}
|
| 19 |
+
|
| 20 |
+
# إعداد تسجيل الأخطاء والمعلومات
|
| 21 |
+
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
|
| 22 |
+
|
| 23 |
+
# إعداد مفتاح API لـ Together
|
| 24 |
+
client = Together(api_key="5ba5c96173d4c62eab6e81edc5abc3f32c4a8c2aa732ef6edbdb2135d27ffdeb")
|
| 25 |
+
|
| 26 |
+
# متغير لتحديد النموذج الذي اختاره المستخدم
|
| 27 |
+
user_selected_model = {}
|
| 28 |
+
|
| 29 |
+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 30 |
+
keyboard = [
|
| 31 |
+
[
|
| 32 |
+
InlineKeyboardButton("Together (FLUX)", callback_data="flux"),
|
| 33 |
+
InlineKeyboardButton("Hugging Face (Stable Diffusion)", callback_data="stable_diffusion")
|
| 34 |
+
]
|
| 35 |
+
]
|
| 36 |
+
reply_markup = InlineKeyboardMarkup(keyboard)
|
| 37 |
+
await update.message.reply_text("اختر النموذج الذي ترغب باستخدامه:", reply_markup=reply_markup)
|
| 38 |
+
|
| 39 |
+
async def set_model(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 40 |
+
query = update.callback_query
|
| 41 |
+
user_id = query.from_user.id
|
| 42 |
+
selected_model = query.data
|
| 43 |
+
user_selected_model[user_id] = selected_model
|
| 44 |
+
await query.answer()
|
| 45 |
+
await query.edit_message_text(text=f"تم اختيار النموذج: {selected_model}")
|
| 46 |
+
|
| 47 |
+
async def generate_image(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 48 |
+
user_id = update.message.from_user.id
|
| 49 |
+
user_prompt = update.message.text
|
| 50 |
+
|
| 51 |
+
# تحقق من النموذج الذي اختاره المستخدم
|
| 52 |
+
model = user_selected_model.get(user_id, "flux")
|
| 53 |
+
|
| 54 |
+
if model == "flux":
|
| 55 |
+
await update.message.reply_text("جاري إنشاء الصورة باستخدام نموذج Together (FLUX)... قد يستغرق ذلك بعض الوقت.")
|
| 56 |
+
try:
|
| 57 |
+
response = client.images.generate(
|
| 58 |
+
prompt=user_prompt,
|
| 59 |
+
model="black-forest-labs/FLUX.1-schnell-Free",
|
| 60 |
+
width=1024,
|
| 61 |
+
height=768,
|
| 62 |
+
steps=1,
|
| 63 |
+
n=1,
|
| 64 |
+
response_format="b64_json"
|
| 65 |
+
)
|
| 66 |
+
if response.data:
|
| 67 |
+
image_base64 = response.data[0].b64_json
|
| 68 |
+
image_data = base64.b64decode(image_base64)
|
| 69 |
+
image = Image.open(BytesIO(image_data))
|
| 70 |
+
with BytesIO() as output:
|
| 71 |
+
image.save(output, format="PNG")
|
| 72 |
+
output.seek(0)
|
| 73 |
+
await context.bot.send_photo(chat_id=update.effective_chat.id, photo=output)
|
| 74 |
+
else:
|
| 75 |
+
await update.message.reply_text("عذرًا، لم أتمكن من توليد صورة باستخدام نموذج Together.")
|
| 76 |
+
except Exception as e:
|
| 77 |
+
await update.message.reply_text(f"حدث خطأ أثناء توليد الصورة باستخدام Together: {e}")
|
| 78 |
+
|
| 79 |
+
elif model == "stable_diffusion":
|
| 80 |
+
await update.message.reply_text("جاري إنشاء الصورة باستخدام نموذج Hugging Face... قد يستغرق ذلك بعض الوقت.")
|
| 81 |
+
try:
|
| 82 |
+
response = requests.post(HUGGING_FACE_API_URL, headers=HUGGING_FACE_HEADERS, json={"inputs": user_prompt})
|
| 83 |
+
if response.status_code == 200:
|
| 84 |
+
image_bytes = response.content
|
| 85 |
+
image = Image.open(BytesIO(image_bytes))
|
| 86 |
+
with BytesIO() as output:
|
| 87 |
+
image.save(output, format="PNG")
|
| 88 |
+
output.seek(0)
|
| 89 |
+
await context.bot.send_photo(chat_id=update.effective_chat.id, photo=output)
|
| 90 |
+
else:
|
| 91 |
+
await update.message.reply_text("عذرًا، لم أتمكن من توليد صورة باستخدام نموذج Hugging Face.")
|
| 92 |
+
except Exception as e:
|
| 93 |
+
await update.message.reply_text(f"حدث خطأ أثناء توليد الصورة باستخدام Hugging Face: {e}")
|
| 94 |
+
|
| 95 |
+
def main():
|
| 96 |
+
# استبدل 'YOUR_TELEGRAM_BOT_TOKEN' بالتوكن الخاص بالبوت
|
| 97 |
+
app = ApplicationBuilder().token('7865424971:AAF_Oe6lu8ZYAl5XIF1M6qU_8MK6GHWEll8').build()
|
| 98 |
+
|
| 99 |
+
app.add_handler(CommandHandler("start", start))
|
| 100 |
+
app.add_handler(CallbackQueryHandler(set_model))
|
| 101 |
+
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, generate_image))
|
| 102 |
+
|
| 103 |
+
app.run_polling()
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
main()
|