Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from telethon import TelegramClient
|
| 3 |
+
from telethon.errors import SessionPasswordNeededError
|
| 4 |
+
import networkx as nx
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
# Obtener credenciales de las variables de entorno
|
| 9 |
+
API_ID = os.getenv("API_ID")
|
| 10 |
+
API_HASH = os.getenv("API_HASH")
|
| 11 |
|
| 12 |
+
# Configurar el cliente de Telegram
|
| 13 |
+
client = TelegramClient('session_name', API_ID, API_HASH)
|
| 14 |
+
|
| 15 |
+
async def fetch_messages(channel_username):
|
| 16 |
+
try:
|
| 17 |
+
await client.start()
|
| 18 |
+
messages = []
|
| 19 |
+
async for message in client.iter_messages(channel_username, limit=50):
|
| 20 |
+
messages.append({
|
| 21 |
+
'id': message.id,
|
| 22 |
+
'text': message.text[:100] + "..." if message.text else "No text",
|
| 23 |
+
'date': str(message.date),
|
| 24 |
+
'forwarded_from': message.forward.sender.username if message.forward else None
|
| 25 |
+
})
|
| 26 |
+
return messages
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error fetching messages: {str(e)}"
|
| 29 |
+
|
| 30 |
+
def build_network(messages):
|
| 31 |
+
G = nx.DiGraph()
|
| 32 |
+
for msg in messages:
|
| 33 |
+
origin = msg['forwarded_from']
|
| 34 |
+
destination = 'TargetChannel' # Canal actual
|
| 35 |
+
if origin and destination:
|
| 36 |
+
if G.has_edge(origin, destination):
|
| 37 |
+
G[origin][destination]['weight'] += 1
|
| 38 |
+
else:
|
| 39 |
+
G.add_edge(origin, destination, weight=1)
|
| 40 |
+
return G
|
| 41 |
+
|
| 42 |
+
def visualize_network(channel_username):
|
| 43 |
+
try:
|
| 44 |
+
import asyncio
|
| 45 |
+
messages = asyncio.run(fetch_messages(channel_username))
|
| 46 |
+
|
| 47 |
+
if isinstance(messages, str): # Si hay un error, devuelve el mensaje
|
| 48 |
+
return messages
|
| 49 |
+
|
| 50 |
+
G = build_network(messages)
|
| 51 |
+
|
| 52 |
+
plt.figure(figsize=(10, 8))
|
| 53 |
+
pos = nx.spring_layout(G)
|
| 54 |
+
nx.draw(G, pos, with_labels=True, node_size=3000, node_color='lightblue', font_size=10, font_weight='bold')
|
| 55 |
+
labels = nx.get_edge_attributes(G, 'weight')
|
| 56 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)
|
| 57 |
+
plt.title("Telegram Channel Network")
|
| 58 |
+
return plt.gcf() # Devuelve la figura actual
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return f"Error: {str(e)}"
|
| 61 |
+
|
| 62 |
+
# Interfaz de Gradio
|
| 63 |
+
demo = gr.Interface(
|
| 64 |
+
fn=visualize_network,
|
| 65 |
+
inputs="text",
|
| 66 |
+
outputs="plot",
|
| 67 |
+
title="Telegram Network Mapper",
|
| 68 |
+
description="Enter a Telegram channel username to analyze its message forwarding network."
|
| 69 |
+
)
|
| 70 |
demo.launch()
|