Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,25 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
app.run_polling()
|
| 104 |
-
|
| 105 |
-
if __name__ == "__main__":
|
| 106 |
-
main()
|
|
|
|
| 1 |
+
import aiohttp
|
| 2 |
+
import asyncio
|
| 3 |
+
|
| 4 |
+
async def send_message_to_telegram(token, chat_id, message):
|
| 5 |
+
url = f"https://api.telegram.org/bot{token}/sendMessage"
|
| 6 |
+
async with aiohttp.ClientSession() as session:
|
| 7 |
+
params = {
|
| 8 |
+
'chat_id': chat_id,
|
| 9 |
+
'text': message
|
| 10 |
+
}
|
| 11 |
+
async with session.get(url, params=params) as response:
|
| 12 |
+
if response.status == 200:
|
| 13 |
+
print("Message sent successfully")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
else:
|
| 15 |
+
print(f"Failed to send message: {response.status}")
|
| 16 |
+
|
| 17 |
+
# مثال لاستخدام الدالة
|
| 18 |
+
async def main():
|
| 19 |
+
token = '7865424971:AAF_Oe6lu8ZYAl5XIF1M6qU_8MK6GHWEll8'
|
| 20 |
+
chat_id = '227321236'
|
| 21 |
+
message = 'Hello, this is a test message from Hugging Face Space!'
|
| 22 |
+
await send_message_to_telegram(token, chat_id, message)
|
| 23 |
+
|
| 24 |
+
# تشغيل الدالة
|
| 25 |
+
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|