Spaces:
Paused
Paused
Commit
·
6a2d7ad
1
Parent(s):
dd1d9aa
add imgbb cdn
Browse files- .gitignore +115 -0
- app.py +7 -6
- bot.session +0 -0
- bot.session-journal +0 -0
- captioner.py +61 -0
- config.yaml +2 -2
- imgbb.py +1 -1
- old.telegram.py +108 -0
- requirements.txt +4 -1
- telegram.py +71 -83
- templates/index.html +1 -1
.gitignore
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ignore environment variable files
|
| 2 |
+
.env
|
| 3 |
+
|
| 4 |
+
# Ignore log files
|
| 5 |
+
bot.log
|
| 6 |
+
|
| 7 |
+
# Ignore Python test scripts
|
| 8 |
+
test.py
|
| 9 |
+
output_image.jpg
|
| 10 |
+
# Ignore cache directory
|
| 11 |
+
cache/
|
| 12 |
+
archive/
|
| 13 |
+
|
| 14 |
+
# Igonre pycache
|
| 15 |
+
__pycache__
|
| 16 |
+
|
| 17 |
+
# Byte-compiled / optimized / DLL files
|
| 18 |
+
__pycache__/
|
| 19 |
+
*.py[cod]
|
| 20 |
+
*$py.class
|
| 21 |
+
|
| 22 |
+
# C extensions
|
| 23 |
+
*.so
|
| 24 |
+
|
| 25 |
+
# Distribution / packaging
|
| 26 |
+
.Python
|
| 27 |
+
build/
|
| 28 |
+
develop-eggs/
|
| 29 |
+
dist/
|
| 30 |
+
downloads/
|
| 31 |
+
eggs/
|
| 32 |
+
.eggs/
|
| 33 |
+
lib/
|
| 34 |
+
lib64/
|
| 35 |
+
parts/
|
| 36 |
+
sdist/
|
| 37 |
+
var/
|
| 38 |
+
wheels/
|
| 39 |
+
share/python-wheels/
|
| 40 |
+
*.egg-info/
|
| 41 |
+
.installed.cfg
|
| 42 |
+
*.egg
|
| 43 |
+
MANIFEST
|
| 44 |
+
|
| 45 |
+
# Virtual environments
|
| 46 |
+
venv/
|
| 47 |
+
ENV/
|
| 48 |
+
env/
|
| 49 |
+
venv.bak/
|
| 50 |
+
venv.bak/
|
| 51 |
+
|
| 52 |
+
# PyInstaller
|
| 53 |
+
# Usually these files are written by a python script from a template
|
| 54 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 55 |
+
*.manifest
|
| 56 |
+
*.spec
|
| 57 |
+
|
| 58 |
+
# Installer logs
|
| 59 |
+
pip-log.txt
|
| 60 |
+
pip-delete-this-directory.txt
|
| 61 |
+
|
| 62 |
+
# Unit test / coverage reports
|
| 63 |
+
htmlcov/
|
| 64 |
+
.tox/
|
| 65 |
+
.nox/
|
| 66 |
+
.coverage
|
| 67 |
+
.coverage.*
|
| 68 |
+
.cache
|
| 69 |
+
nosetests.xml
|
| 70 |
+
coverage.xml
|
| 71 |
+
*.cover
|
| 72 |
+
*.py,cover
|
| 73 |
+
.hypothesis/
|
| 74 |
+
.pytest_cache/
|
| 75 |
+
|
| 76 |
+
# Translations
|
| 77 |
+
*.mo
|
| 78 |
+
*.pot
|
| 79 |
+
|
| 80 |
+
# Django stuff:
|
| 81 |
+
*.log
|
| 82 |
+
local_settings.py
|
| 83 |
+
db.sqlite3
|
| 84 |
+
|
| 85 |
+
# Flask stuff:
|
| 86 |
+
instance/
|
| 87 |
+
.webassets-cache
|
| 88 |
+
|
| 89 |
+
# Scrapy stuff:
|
| 90 |
+
.scrapy
|
| 91 |
+
|
| 92 |
+
# Sphinx documentation
|
| 93 |
+
docs/_build/
|
| 94 |
+
|
| 95 |
+
# PyBuilder
|
| 96 |
+
target/
|
| 97 |
+
|
| 98 |
+
# IPython
|
| 99 |
+
profile_default/
|
| 100 |
+
ipython_config.py
|
| 101 |
+
|
| 102 |
+
# pyenv
|
| 103 |
+
# For a library or package, you might want to ignore these files since the code is intended to run in multiple environments;
|
| 104 |
+
# otherwise, check them in:
|
| 105 |
+
# .python-version
|
| 106 |
+
|
| 107 |
+
# pipenv
|
| 108 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 109 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 110 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 111 |
+
# install all needed dependencies.
|
| 112 |
+
Pipfile.lock
|
| 113 |
+
|
| 114 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
| 115 |
+
__pypackages__/
|
app.py
CHANGED
|
@@ -4,8 +4,9 @@ import os
|
|
| 4 |
import threading
|
| 5 |
import yaml
|
| 6 |
from image_payload_reader import read_payload_from_image
|
| 7 |
-
from telegram import start_telegram_bot
|
| 8 |
from utils import read_config
|
|
|
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
templates_dir = "templates"
|
|
@@ -39,8 +40,8 @@ def start_bots():
|
|
| 39 |
telegram_bot_enabled = info['telegram_bot_enabled']
|
| 40 |
|
| 41 |
if telegram_bot_enabled:
|
| 42 |
-
start_telegram_bot()
|
| 43 |
-
|
| 44 |
# Note: WhatsApp bot functionality is not implemented in this Flask version
|
| 45 |
|
| 46 |
# Start the Telegram bot in a separate thread when the application starts
|
|
@@ -48,7 +49,7 @@ threading.Thread(target=start_bots).start()
|
|
| 48 |
|
| 49 |
# Function to fetch image files and corresponding YAML data from 'cache' directory
|
| 50 |
def fetch_image_files():
|
| 51 |
-
files =
|
| 52 |
image_files = [f for f in files if f.endswith(('.jpg', '.jpeg', '.png'))]
|
| 53 |
response_data = []
|
| 54 |
|
|
@@ -82,7 +83,7 @@ def get_images():
|
|
| 82 |
|
| 83 |
@app.route('/cache/<image_name>')
|
| 84 |
def get_image(image_name):
|
| 85 |
-
file_path =
|
| 86 |
if os.path.exists(file_path):
|
| 87 |
return send_file(file_path)
|
| 88 |
return jsonify({"error": "File not found"}), 404
|
|
@@ -96,7 +97,7 @@ def get_static_image(image_name):
|
|
| 96 |
|
| 97 |
@app.route('/yaml/<image_name>')
|
| 98 |
def get_embedded_yaml(image_name):
|
| 99 |
-
file_path =
|
| 100 |
if not os.path.exists(file_path):
|
| 101 |
return jsonify({"error": "Image file not found"}), 404
|
| 102 |
|
|
|
|
| 4 |
import threading
|
| 5 |
import yaml
|
| 6 |
from image_payload_reader import read_payload_from_image
|
| 7 |
+
#from telegram import start_telegram_bot
|
| 8 |
from utils import read_config
|
| 9 |
+
from imgbb import extract_original_image_urls
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
templates_dir = "templates"
|
|
|
|
| 40 |
telegram_bot_enabled = info['telegram_bot_enabled']
|
| 41 |
|
| 42 |
if telegram_bot_enabled:
|
| 43 |
+
#start_telegram_bot()
|
| 44 |
+
pass
|
| 45 |
# Note: WhatsApp bot functionality is not implemented in this Flask version
|
| 46 |
|
| 47 |
# Start the Telegram bot in a separate thread when the application starts
|
|
|
|
| 49 |
|
| 50 |
# Function to fetch image files and corresponding YAML data from 'cache' directory
|
| 51 |
def fetch_image_files():
|
| 52 |
+
files = extract_original_image_urls()
|
| 53 |
image_files = [f for f in files if f.endswith(('.jpg', '.jpeg', '.png'))]
|
| 54 |
response_data = []
|
| 55 |
|
|
|
|
| 83 |
|
| 84 |
@app.route('/cache/<image_name>')
|
| 85 |
def get_image(image_name):
|
| 86 |
+
file_path = image_name
|
| 87 |
if os.path.exists(file_path):
|
| 88 |
return send_file(file_path)
|
| 89 |
return jsonify({"error": "File not found"}), 404
|
|
|
|
| 97 |
|
| 98 |
@app.route('/yaml/<image_name>')
|
| 99 |
def get_embedded_yaml(image_name):
|
| 100 |
+
file_path = image_name
|
| 101 |
if not os.path.exists(file_path):
|
| 102 |
return jsonify({"error": "Image file not found"}), 404
|
| 103 |
|
bot.session
ADDED
|
Binary file (28.7 kB). View file
|
|
|
bot.session-journal
ADDED
|
Binary file (4.62 kB). View file
|
|
|
captioner.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import logging
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
# Load environment variables from a .env file
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Configure logging
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
# Define constants
|
| 15 |
+
API_ENDPOINT = os.getenv("API_ENDPOINT", "https://chandimaprabath-florence-2-sd3-captioner-cpu.hf.space/generate")
|
| 16 |
+
|
| 17 |
+
def get_image_caption(image_path):
|
| 18 |
+
"""
|
| 19 |
+
Send a POST request to the API endpoint with the image and return the caption.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
image_path (str): Path to the image file.
|
| 23 |
+
api_endpoint (str): URL of the API endpoint.
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
str: The caption generated by the API.
|
| 27 |
+
"""
|
| 28 |
+
try:
|
| 29 |
+
with open(image_path, "rb") as image_file:
|
| 30 |
+
files = {"image": image_file}
|
| 31 |
+
|
| 32 |
+
# Start timer
|
| 33 |
+
start_time = time.time()
|
| 34 |
+
|
| 35 |
+
response = requests.post(API_ENDPOINT, files=files)
|
| 36 |
+
response.raise_for_status() # Raise HTTPError for bad responses
|
| 37 |
+
|
| 38 |
+
# End timer
|
| 39 |
+
end_time = time.time()
|
| 40 |
+
elapsed_time = end_time - start_time
|
| 41 |
+
|
| 42 |
+
data = response.json()
|
| 43 |
+
caption = data.get('caption', 'No caption found')
|
| 44 |
+
|
| 45 |
+
return caption, elapsed_time
|
| 46 |
+
except requests.exceptions.RequestException as e:
|
| 47 |
+
logger.error(f"Request failed: {e}")
|
| 48 |
+
return None, None
|
| 49 |
+
except Exception as e:
|
| 50 |
+
logger.error(f"An error occurred: {e}")
|
| 51 |
+
return None, None
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
IMAGE_PATH = "alchemy_refiner_alchemy_magic_0_3ed4af60-070e-47ed-b3de-a158103efa7b_0.jpg"
|
| 55 |
+
# Get caption for the specified image
|
| 56 |
+
caption, elapsed_time = get_image_caption(IMAGE_PATH)
|
| 57 |
+
if caption:
|
| 58 |
+
logger.info(f"Caption: {caption}")
|
| 59 |
+
logger.info(f"Time taken: {elapsed_time:.2f} seconds")
|
| 60 |
+
else:
|
| 61 |
+
logger.error("Failed to get caption.")
|
config.yaml
CHANGED
|
@@ -21,7 +21,7 @@ config:
|
|
| 21 |
cfg_scale: 7.0
|
| 22 |
imgbb_upload: false
|
| 23 |
app:
|
| 24 |
-
whatsapp_bot_enabled:
|
| 25 |
-
telegram_bot_enabled:
|
| 26 |
version: "0.6 Beta"
|
| 27 |
debug: true
|
|
|
|
| 21 |
cfg_scale: 7.0
|
| 22 |
imgbb_upload: false
|
| 23 |
app:
|
| 24 |
+
whatsapp_bot_enabled: false
|
| 25 |
+
telegram_bot_enabled: false
|
| 26 |
version: "0.6 Beta"
|
| 27 |
debug: true
|
imgbb.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import requests
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
|
| 4 |
-
def extract_original_image_urls(url):
|
| 5 |
try:
|
| 6 |
# Make a GET request to fetch the raw HTML content
|
| 7 |
response = requests.get(url)
|
|
|
|
| 1 |
import requests
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
|
| 4 |
+
def extract_original_image_urls(url='https://unicone-studio.imgbb.com/'):
|
| 5 |
try:
|
| 6 |
# Make a GET request to fetch the raw HTML content
|
| 7 |
response = requests.get(url)
|
old.telegram.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import os
|
| 3 |
+
import threading
|
| 4 |
+
import telebot
|
| 5 |
+
from llm import generate_llm # Assuming these are your custom functions
|
| 6 |
+
from sd import generate_sd # Replace with actual imports as needed
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
# Load environment variables
|
| 10 |
+
def load_env():
|
| 11 |
+
global TELEGRAM_BOT_TOKEN
|
| 12 |
+
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 13 |
+
if not TELEGRAM_BOT_TOKEN:
|
| 14 |
+
raise ValueError("TELEGRAM_BOT_TOKEN environment variable is not set")
|
| 15 |
+
|
| 16 |
+
load_env()
|
| 17 |
+
|
| 18 |
+
# Initialize the bot
|
| 19 |
+
bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN)
|
| 20 |
+
|
| 21 |
+
# Configure logging
|
| 22 |
+
LOG_FILE = "bot.log"
|
| 23 |
+
logging.basicConfig(
|
| 24 |
+
level=logging.INFO,
|
| 25 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 26 |
+
handlers=[
|
| 27 |
+
logging.FileHandler(LOG_FILE),
|
| 28 |
+
logging.StreamHandler()
|
| 29 |
+
]
|
| 30 |
+
)
|
| 31 |
+
logger = logging.getLogger(__name__)
|
| 32 |
+
|
| 33 |
+
# Configure SOCKS5 Proxy (replace with your proxy details)
|
| 34 |
+
PROXY_HOST = '23.19.244.109'
|
| 35 |
+
PROXY_PORT = 1080
|
| 36 |
+
|
| 37 |
+
telebot.apihelper.proxy = {'https': f'socks5://{PROXY_HOST}:{PROXY_PORT}'}
|
| 38 |
+
|
| 39 |
+
def response_text(chat_id, prompt):
|
| 40 |
+
"""
|
| 41 |
+
Generate a response using the LLM and send it to the user.
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
msg = generate_llm(prompt) # Example function call to generate a response
|
| 45 |
+
bot.send_message(chat_id, msg)
|
| 46 |
+
logger.info(f"Chat ID: {chat_id} - Sent Message: {msg}")
|
| 47 |
+
except Exception as e:
|
| 48 |
+
logger.error(f"Chat ID: {chat_id} - Error in response_text: {e}")
|
| 49 |
+
bot.send_message(chat_id, "There was an error processing your request.")
|
| 50 |
+
|
| 51 |
+
@bot.message_handler(commands=['imagine'])
|
| 52 |
+
def handle_imagine_command(message):
|
| 53 |
+
"""
|
| 54 |
+
Handle the /imagine command by generating an image from the provided prompt.
|
| 55 |
+
"""
|
| 56 |
+
prompt = message.text.replace('/imagine', '').strip()
|
| 57 |
+
if not prompt:
|
| 58 |
+
bot.reply_to(message, "Please provide a prompt after /imagine.")
|
| 59 |
+
logger.info(f"Chat ID: {message.chat.id} - Sent Message: Please provide a prompt after /imagine.")
|
| 60 |
+
return
|
| 61 |
+
|
| 62 |
+
generating_message = bot.send_message(message.chat.id, "Generating...")
|
| 63 |
+
logger.info(f"Chat ID: {message.chat.id} - Sent Message: Generating...")
|
| 64 |
+
|
| 65 |
+
def handle_image_generation():
|
| 66 |
+
try:
|
| 67 |
+
# Example function call to generate image data
|
| 68 |
+
image_data, image_path = generate_sd(prompt)
|
| 69 |
+
bot.delete_message(chat_id=message.chat.id, message_id=generating_message.message_id)
|
| 70 |
+
|
| 71 |
+
if image_data:
|
| 72 |
+
bot.send_photo(message.chat.id, image_data)
|
| 73 |
+
logger.info(f"Chat ID: {message.chat.id} - Sent Image {image_path}")
|
| 74 |
+
else:
|
| 75 |
+
bot.reply_to(message, "Failed to generate image. Please try again later.")
|
| 76 |
+
logger.error(f"Chat ID: {message.chat.id} - Failed to generate image.")
|
| 77 |
+
except Exception as e:
|
| 78 |
+
logger.error(f"Chat ID: {message.chat.id} - Error in handle_image_generation: {e}")
|
| 79 |
+
bot.reply_to(message, "There was an error generating the image. Please try again later.")
|
| 80 |
+
|
| 81 |
+
threading.Thread(target=handle_image_generation).start()
|
| 82 |
+
|
| 83 |
+
@bot.message_handler(func=lambda message: True)
|
| 84 |
+
def handle_message(message):
|
| 85 |
+
"""
|
| 86 |
+
Handle all text messages by generating a response and sending it.
|
| 87 |
+
"""
|
| 88 |
+
chat_id = message.chat.id
|
| 89 |
+
user_message = message.text
|
| 90 |
+
logger.info(f"Chat ID: {chat_id} - Received Message: {user_message}")
|
| 91 |
+
|
| 92 |
+
threading.Thread(target=response_text, args=(chat_id, user_message)).start()
|
| 93 |
+
|
| 94 |
+
def start_telegram_bot():
|
| 95 |
+
"""
|
| 96 |
+
Start the Telegram bot.
|
| 97 |
+
"""
|
| 98 |
+
while True:
|
| 99 |
+
try:
|
| 100 |
+
logger.info("Starting bot")
|
| 101 |
+
bot.polling(none_stop=True)
|
| 102 |
+
except Exception as e:
|
| 103 |
+
logger.critical(f"Critical error: {e}")
|
| 104 |
+
time.sleep(5) # Wait for a few seconds before restarting
|
| 105 |
+
|
| 106 |
+
# Ensure the bot starts when this script is executed directly
|
| 107 |
+
if __name__ == '__main__':
|
| 108 |
+
start_telegram_bot()
|
requirements.txt
CHANGED
|
@@ -6,4 +6,7 @@ pillow
|
|
| 6 |
pyTelegramBotAPI
|
| 7 |
PyYAML
|
| 8 |
watchdog
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
pyTelegramBotAPI
|
| 7 |
PyYAML
|
| 8 |
watchdog
|
| 9 |
+
telethon
|
| 10 |
+
asyncio
|
| 11 |
+
python-socks[asyncio]
|
| 12 |
+
beautifulsoup4
|
telegram.py
CHANGED
|
@@ -1,23 +1,22 @@
|
|
| 1 |
import logging
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
from llm import generate_llm # Assuming these are your custom functions
|
| 6 |
from sd import generate_sd # Replace with actual imports as needed
|
| 7 |
-
import
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
def load_env():
|
| 11 |
-
global TELEGRAM_BOT_TOKEN
|
|
|
|
|
|
|
| 12 |
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 13 |
-
if not TELEGRAM_BOT_TOKEN:
|
| 14 |
-
raise ValueError("
|
| 15 |
|
| 16 |
load_env()
|
| 17 |
|
| 18 |
-
# Initialize the bot
|
| 19 |
-
bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN)
|
| 20 |
-
|
| 21 |
# Configure logging
|
| 22 |
LOG_FILE = "bot.log"
|
| 23 |
logging.basicConfig(
|
|
@@ -30,79 +29,68 @@ logging.basicConfig(
|
|
| 30 |
)
|
| 31 |
logger = logging.getLogger(__name__)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
logger.info(f"Chat ID: {chat_id} - Sent Message: {msg}")
|
| 47 |
-
except Exception as e:
|
| 48 |
-
logger.error(f"Chat ID: {chat_id} - Error in response_text: {e}")
|
| 49 |
-
bot.send_message(chat_id, "There was an error processing your request.")
|
| 50 |
-
|
| 51 |
-
@bot.message_handler(commands=['imagine'])
|
| 52 |
-
def handle_imagine_command(message):
|
| 53 |
-
"""
|
| 54 |
-
Handle the /imagine command by generating an image from the provided prompt.
|
| 55 |
-
"""
|
| 56 |
-
prompt = message.text.replace('/imagine', '').strip()
|
| 57 |
-
if not prompt:
|
| 58 |
-
bot.reply_to(message, "Please provide a prompt after /imagine.")
|
| 59 |
-
logger.info(f"Chat ID: {message.chat.id} - Sent Message: Please provide a prompt after /imagine.")
|
| 60 |
-
return
|
| 61 |
-
|
| 62 |
-
generating_message = bot.send_message(message.chat.id, "Generating...")
|
| 63 |
-
logger.info(f"Chat ID: {message.chat.id} - Sent Message: Generating...")
|
| 64 |
-
|
| 65 |
-
def handle_image_generation():
|
| 66 |
-
try:
|
| 67 |
-
# Example function call to generate image data
|
| 68 |
-
image_data, image_path = generate_sd(prompt)
|
| 69 |
-
bot.delete_message(chat_id=message.chat.id, message_id=generating_message.message_id)
|
| 70 |
-
|
| 71 |
-
if image_data:
|
| 72 |
-
bot.send_photo(message.chat.id, image_data)
|
| 73 |
-
logger.info(f"Chat ID: {message.chat.id} - Sent Image {image_path}")
|
| 74 |
-
else:
|
| 75 |
-
bot.reply_to(message, "Failed to generate image. Please try again later.")
|
| 76 |
-
logger.error(f"Chat ID: {message.chat.id} - Failed to generate image.")
|
| 77 |
-
except Exception as e:
|
| 78 |
-
logger.error(f"Chat ID: {message.chat.id} - Error in handle_image_generation: {e}")
|
| 79 |
-
bot.reply_to(message, "There was an error generating the image. Please try again later.")
|
| 80 |
-
|
| 81 |
-
threading.Thread(target=handle_image_generation).start()
|
| 82 |
-
|
| 83 |
-
@bot.message_handler(func=lambda message: True)
|
| 84 |
-
def handle_message(message):
|
| 85 |
-
"""
|
| 86 |
-
Handle all text messages by generating a response and sending it.
|
| 87 |
-
"""
|
| 88 |
-
chat_id = message.chat.id
|
| 89 |
-
user_message = message.text
|
| 90 |
-
logger.info(f"Chat ID: {chat_id} - Received Message: {user_message}")
|
| 91 |
-
|
| 92 |
-
threading.Thread(target=response_text, args=(chat_id, user_message)).start()
|
| 93 |
-
|
| 94 |
-
def start_telegram_bot():
|
| 95 |
-
"""
|
| 96 |
-
Start the Telegram bot.
|
| 97 |
-
"""
|
| 98 |
-
while True:
|
| 99 |
-
try:
|
| 100 |
-
logger.info("Starting bot")
|
| 101 |
-
bot.polling(none_stop=True)
|
| 102 |
-
except Exception as e:
|
| 103 |
-
logger.critical(f"Critical error: {e}")
|
| 104 |
-
time.sleep(5) # Wait for a few seconds before restarting
|
| 105 |
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
if __name__ == '__main__':
|
| 108 |
-
|
|
|
|
| 1 |
import logging
|
| 2 |
import os
|
| 3 |
+
import asyncio
|
| 4 |
+
from telethon import TelegramClient, events, sync
|
| 5 |
from llm import generate_llm # Assuming these are your custom functions
|
| 6 |
from sd import generate_sd # Replace with actual imports as needed
|
| 7 |
+
import socks
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
def load_env():
|
| 11 |
+
global TELEGRAM_API_ID, TELEGRAM_API_HASH, TELEGRAM_BOT_TOKEN
|
| 12 |
+
TELEGRAM_API_ID = int(os.getenv("TELEGRAM_API_ID"))
|
| 13 |
+
TELEGRAM_API_HASH = os.getenv("TELEGRAM_API_HASH")
|
| 14 |
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 15 |
+
if not all([TELEGRAM_API_ID, TELEGRAM_API_HASH, TELEGRAM_BOT_TOKEN]):
|
| 16 |
+
raise ValueError("One or more required environment variables are not set")
|
| 17 |
|
| 18 |
load_env()
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
# Configure logging
|
| 21 |
LOG_FILE = "bot.log"
|
| 22 |
logging.basicConfig(
|
|
|
|
| 29 |
)
|
| 30 |
logger = logging.getLogger(__name__)
|
| 31 |
|
| 32 |
+
proxy_server = '142.93.68.63'
|
| 33 |
+
proxy_port = 2434
|
| 34 |
+
proxy_secret = 'ee32b920dffb51643028e2f6b878d4eac1666172616b61762e636f6d'
|
| 35 |
+
proxy_dc_id = 2 # This is usually 2 for MTProto proxies
|
| 36 |
+
|
| 37 |
+
proxy = (
|
| 38 |
+
socks.SOCKS5,
|
| 39 |
+
proxy_server,
|
| 40 |
+
proxy_port,
|
| 41 |
+
True,
|
| 42 |
+
'vpn',
|
| 43 |
+
'unlimited'
|
| 44 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
async def main():
|
| 47 |
+
# Initialize Telethon client
|
| 48 |
+
client = TelegramClient('bot', TELEGRAM_API_ID, TELEGRAM_API_HASH, proxy=proxy).start(TELEGRAM_BOT_TOKEN)
|
| 49 |
+
|
| 50 |
+
@client.on(events.NewMessage)
|
| 51 |
+
async def handle_message(event):
|
| 52 |
+
chat_id = event.chat_id
|
| 53 |
+
user_message = event.raw_text
|
| 54 |
+
logger.info(f"Chat ID: {chat_id} - Received Message: {user_message}")
|
| 55 |
+
|
| 56 |
+
# Handle /imagine command
|
| 57 |
+
if user_message.startswith('/imagine'):
|
| 58 |
+
prompt = user_message.replace('/imagine', '').strip()
|
| 59 |
+
if not prompt:
|
| 60 |
+
await event.reply("Please provide a prompt after /imagine.")
|
| 61 |
+
logger.info(f"Chat ID: {chat_id} - Sent Message: Please provide a prompt after /imagine.")
|
| 62 |
+
return
|
| 63 |
+
|
| 64 |
+
generating_message = await event.reply("Generating...")
|
| 65 |
+
logger.info(f"Chat ID: {chat_id} - Sent Message: Generating...")
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
# Example function call to generate image data
|
| 69 |
+
image_data, image_path = generate_sd(prompt)
|
| 70 |
+
await client.delete_messages(chat_id, [generating_message.id])
|
| 71 |
+
|
| 72 |
+
if image_data:
|
| 73 |
+
await client.send_file(chat_id, image_data)
|
| 74 |
+
logger.info(f"Chat ID: {chat_id} - Sent Image {image_path}")
|
| 75 |
+
else:
|
| 76 |
+
await event.reply("Failed to generate image. Please try again later.")
|
| 77 |
+
logger.error(f"Chat ID: {chat_id} - Failed to generate image.")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
logger.error(f"Chat ID: {chat_id} - Error in handle_image_generation: {e}")
|
| 80 |
+
await event.reply("There was an error generating the image. Please try again later.")
|
| 81 |
+
|
| 82 |
+
else:
|
| 83 |
+
# Default response handling
|
| 84 |
+
try:
|
| 85 |
+
msg = generate_llm(user_message) # Example function call to generate a response
|
| 86 |
+
await event.reply(msg)
|
| 87 |
+
logger.info(f"Chat ID: {chat_id} - Sent Message: {msg}")
|
| 88 |
+
except Exception as e:
|
| 89 |
+
logger.error(f"Chat ID: {chat_id} - Error in response_text: {e}")
|
| 90 |
+
await event.reply("There was an error processing your request.")
|
| 91 |
+
|
| 92 |
+
await client.run_until_disconnected()
|
| 93 |
+
|
| 94 |
+
# Run the main function
|
| 95 |
if __name__ == '__main__':
|
| 96 |
+
asyncio.run(main())
|
templates/index.html
CHANGED
|
@@ -268,7 +268,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 268 |
container.className = 'image-item';
|
| 269 |
|
| 270 |
const img = document.createElement('img');
|
| 271 |
-
img.src =
|
| 272 |
img.alt = image;
|
| 273 |
img.onerror = () => {
|
| 274 |
container.style.display = 'none';
|
|
|
|
| 268 |
container.className = 'image-item';
|
| 269 |
|
| 270 |
const img = document.createElement('img');
|
| 271 |
+
img.src = `${image}`;
|
| 272 |
img.alt = image;
|
| 273 |
img.onerror = () => {
|
| 274 |
container.style.display = 'none';
|