Spaces:
Paused
Paused
measmonysuon
commited on
Commit
•
d6d4b5a
1
Parent(s):
e3f3739
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,8 @@
|
|
1 |
-
import telebot
|
2 |
import gradio as gr
|
3 |
from gradio_client import Client
|
4 |
import os
|
5 |
import logging
|
6 |
-
import
|
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,10 +21,6 @@ DEFAULT_STYLE = "3840 x 2160"
|
|
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
|
@@ -93,6 +56,11 @@ def gradio_interface(prompt, resolution_key):
|
|
93 |
else:
|
94 |
return None, "There was an error processing your photo. Please try again later."
|
95 |
|
|
|
|
|
|
|
|
|
|
|
96 |
def create_gradio_interface(username):
|
97 |
description = f"Welcome, {username}! Generate images based on prompts."
|
98 |
|
@@ -128,36 +96,13 @@ def launch_gradio(username=None):
|
|
128 |
interface = create_gradio_interface(username)
|
129 |
interface.launch()
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
username =
|
135 |
-
|
136 |
-
|
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 |
-
|
163 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from gradio_client import Client
|
3 |
import os
|
4 |
import logging
|
5 |
+
from urllib.parse import parse_qs, urlparse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Initialize the client for image generation
|
8 |
client_image = Client("mukaist/DALLE-4K")
|
|
|
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
|
|
|
56 |
else:
|
57 |
return None, "There was an error processing your photo. Please try again later."
|
58 |
|
59 |
+
def extract_username_from_url(url):
|
60 |
+
parsed_url = urlparse(url)
|
61 |
+
params = parse_qs(parsed_url.query)
|
62 |
+
return params.get('username', ["Guest"])[0]
|
63 |
+
|
64 |
def create_gradio_interface(username):
|
65 |
description = f"Welcome, {username}! Generate images based on prompts."
|
66 |
|
|
|
96 |
interface = create_gradio_interface(username)
|
97 |
interface.launch()
|
98 |
|
99 |
+
def get_username_from_url():
|
100 |
+
# Placeholder URL, replace with actual URL or method to get URL in deployment
|
101 |
+
url = 'https://measmonysuon-webapp.hf.space/?username=MoMo'
|
102 |
+
username = extract_username_from_url(url)
|
103 |
+
logger.info(f"Extracted username: {username}")
|
104 |
+
return username
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
# Launch Gradio with the extracted or default username
|
107 |
+
username = get_username_from_url()
|
108 |
+
launch_gradio(username)
|