measmonysuon commited on
Commit
656d9b8
1 Parent(s): db8462d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -6
app.py CHANGED
@@ -1,8 +1,41 @@
1
- import subprocess
2
  import gradio as gr
3
  from gradio_client import Client
4
  import os
5
  import logging
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Initialize the client for image generation
8
  client_image = Client("mukaist/DALLE-4K")
@@ -21,6 +54,10 @@ DEFAULT_STYLE = "3840 x 2160"
21
  logging.basicConfig(level=logging.INFO)
22
  logger = logging.getLogger(__name__)
23
 
 
 
 
 
24
  def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
25
  resolution = resolutions.get(resolution_key, (1024, 1024))
26
  width, height = resolution
@@ -87,14 +124,40 @@ def create_gradio_interface(username):
87
  def launch_gradio(username=None):
88
  if not username:
89
  username = "Guest"
90
-
91
  # Launch the Gradio interface
92
  interface = create_gradio_interface(username)
93
  interface.launch()
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  if __name__ == "__main__":
96
- # Start the Gradio interface
97
- launch_gradio(username="Guest")
 
98
 
99
- # Start the Telegram bot
100
- subprocess.Popen(["python", "telegrabot.py"])
 
1
+ import telebot
2
  import gradio as gr
3
  from gradio_client import Client
4
  import os
5
  import logging
6
+ import socks
7
+ import socket
8
+ import requests
9
+ from requests.adapters import HTTPAdapter
10
+ from requests.packages.urllib3.util.retry import Retry
11
+ import threading
12
+
13
+ # Proxy configuration
14
+ PROXY_HOST = '64.29.87.81'
15
+ PROXY_PORT = 48558
16
+ PROXY_USERNAME = 'KNaDX4iRd2t6El0'
17
+ PROXY_PASSWORD = 'NleyoQHQJwHbqSH'
18
+
19
+ # Configure SOCKS proxy
20
+ socks.set_default_proxy(socks.SOCKS5, PROXY_HOST, PROXY_PORT, username=PROXY_USERNAME, password=PROXY_PASSWORD)
21
+ socket.socket = socks.socksocket
22
+
23
+ # Create a custom requests session with proxy
24
+ session = requests.Session()
25
+ session.proxies.update({
26
+ 'http': f'socks5://{PROXY_USERNAME}:{PROXY_PASSWORD}@{PROXY_HOST}:{PROXY_PORT}',
27
+ 'https': f'socks5://{PROXY_USERNAME}:{PROXY_PASSWORD}@{PROXY_HOST}:{PROXY_PORT}',
28
+ })
29
+
30
+ # Add retry strategy to the session
31
+ retry_strategy = Retry(
32
+ total=5,
33
+ backoff_factor=1,
34
+ status_forcelist=[500, 502, 503, 504]
35
+ )
36
+ adapter = HTTPAdapter(max_retries=retry_strategy)
37
+ session.mount('http://', adapter)
38
+ session.mount('https://', adapter)
39
 
40
  # Initialize the client for image generation
41
  client_image = Client("mukaist/DALLE-4K")
 
54
  logging.basicConfig(level=logging.INFO)
55
  logger = logging.getLogger(__name__)
56
 
57
+ # Initialize the bot with your API token
58
+ API_TOKEN = 'YOUR_TELEGRAM_BOT_API_TOKEN'
59
+ bot = telebot.TeleBot(API_TOKEN)
60
+
61
  def generate_image(prompt, resolution_key, style=DEFAULT_STYLE):
62
  resolution = resolutions.get(resolution_key, (1024, 1024))
63
  width, height = resolution
 
124
  def launch_gradio(username=None):
125
  if not username:
126
  username = "Guest"
 
127
  # Launch the Gradio interface
128
  interface = create_gradio_interface(username)
129
  interface.launch()
130
 
131
+ @bot.message_handler(commands=['start'])
132
+ def handle_start(message):
133
+ # Extract username from message
134
+ username = message.from_user.username or "Guest"
135
+
136
+ # Construct URL with username
137
+ encoded_url = f"https://measmonysuon-webapp.hf.space/?username={username}"
138
+
139
+ # Send a message with the inline button to open the web app
140
+ markup = telebot.types.InlineKeyboardMarkup()
141
+ button = telebot.types.InlineKeyboardButton(
142
+ text="Open Web App",
143
+ web_app=telebot.types.WebAppInfo(url=encoded_url)
144
+ )
145
+ markup.add(button)
146
+
147
+ bot.send_message(
148
+ message.chat.id,
149
+ f"Welcome, {username}! Click the button below to open the web app:",
150
+ reply_markup=markup
151
+ )
152
+
153
+ def run_telegram_bot():
154
+ bot.polling()
155
+
156
+ # Run Gradio and Telegram bot concurrently
157
  if __name__ == "__main__":
158
+ # Launch Gradio in a separate thread
159
+ gradio_thread = threading.Thread(target=lambda: launch_gradio("Guest"))
160
+ gradio_thread.start()
161
 
162
+ # Start Telegram bot polling
163
+ run_telegram_bot()