Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,213 +1,187 @@
|
|
| 1 |
-
"""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
OPENROUTER_API_KEY = os.getenv(
|
| 15 |
-
"OPENROUTER_API_KEY",
|
| 16 |
-
"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"
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
# ------------------------------------------------------------------
|
| 20 |
-
# Ottieni modelli raggruppati
|
| 21 |
-
# ------------------------------------------------------------------
|
| 22 |
-
@functools.lru_cache(maxsize=1)
|
| 23 |
-
def fetch_models_grouped() -> dict:
|
| 24 |
-
headers = {"Authorization": f"Bearer {OPENROUTER_API_KEY}"}
|
| 25 |
-
try:
|
| 26 |
-
resp = requests.get("https://openrouter.ai/api/v1/models", headers=headers, timeout=15)
|
| 27 |
-
resp.raise_for_status()
|
| 28 |
-
data = resp.json()
|
| 29 |
-
|
| 30 |
-
reasoning_models = []
|
| 31 |
-
casual_models = []
|
| 32 |
-
|
| 33 |
-
for m in data["data"]:
|
| 34 |
-
model_id = m["id"].lower()
|
| 35 |
-
if any(key in model_id for key in ["gpt", "claude", "llama", "mistral", "mixtral", "gemini", "command-r", "qwen"]):
|
| 36 |
-
reasoning_models.append(m["id"])
|
| 37 |
-
else:
|
| 38 |
-
casual_models.append(m["id"])
|
| 39 |
-
|
| 40 |
-
return {
|
| 41 |
-
"reasoning": sorted(reasoning_models),
|
| 42 |
-
"casual": sorted(casual_models)
|
| 43 |
-
}
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return {
|
| 46 |
-
"reasoning": ["openai/gpt-4-turbo"],
|
| 47 |
-
"casual": []
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
# ------------------------------------------------------------------
|
| 51 |
-
# Format cronologia
|
| 52 |
-
# ------------------------------------------------------------------
|
| 53 |
-
def format_history(history: list) -> str:
|
| 54 |
-
output = ""
|
| 55 |
-
for msg in history:
|
| 56 |
-
if msg["role"] == "user":
|
| 57 |
-
output += f"π§βπ» **Tu**:\n{msg['content']}\n\n"
|
| 58 |
-
elif msg["role"] == "assistant":
|
| 59 |
-
output += f"π€ **Assistente**:\n{msg['content']}\n\n"
|
| 60 |
-
return output.strip()
|
| 61 |
-
|
| 62 |
-
# ------------------------------------------------------------------
|
| 63 |
-
# Chat
|
| 64 |
-
# ------------------------------------------------------------------
|
| 65 |
-
def chat_with_openrouter(prompt: str, selected_model: str, history: list):
|
| 66 |
-
headers = {
|
| 67 |
-
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 68 |
-
"Content-Type": "application/json"
|
| 69 |
-
}
|
| 70 |
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
def
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" OpenRouter Chatbot β con storico conversazione + modelli separati + Telegram Run: python app.py """
|
| 2 |
+
|
| 3 |
+
import os import functools import threading import requests import gradio as gr
|
| 4 |
+
|
| 5 |
+
from telegram import Update from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes
|
| 6 |
+
|
| 7 |
+
------------------------------------------------------------------
|
| 8 |
+
|
| 9 |
+
Configurazione
|
| 10 |
+
|
| 11 |
+
------------------------------------------------------------------
|
| 12 |
+
|
| 13 |
+
OPENROUTER_API_KEY = os.getenv( "OPENROUTER_API_KEY", "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" ) TELEGRAM_BOT_TOKEN = "7873487915:AAFZYgAlt5-qr7jdXG1tKkG9KnAZeP6uC8w"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
------------------------------------------------------------------
|
| 16 |
|
| 17 |
+
Ottieni modelli raggruppati
|
| 18 |
+
|
| 19 |
+
------------------------------------------------------------------
|
| 20 |
+
|
| 21 |
+
@functools.lru_cache(maxsize=1) def fetch_models_grouped() -> dict: headers = {"Authorization": f"Bearer {OPENROUTER_API_KEY}"} try: resp = requests.get("https://openrouter.ai/api/v1/models", headers=headers, timeout=15) resp.raise_for_status() data = resp.json()
|
| 22 |
+
|
| 23 |
+
reasoning_models = []
|
| 24 |
+
casual_models = []
|
| 25 |
+
|
| 26 |
+
for m in data["data"]:
|
| 27 |
+
model_id = m["id"].lower()
|
| 28 |
+
if any(key in model_id for key in ["gpt", "claude", "llama", "mistral", "mixtral", "gemini", "command-r", "qwen"]):
|
| 29 |
+
reasoning_models.append(m["id"])
|
| 30 |
+
else:
|
| 31 |
+
casual_models.append(m["id"])
|
| 32 |
+
|
| 33 |
+
return {
|
| 34 |
+
"reasoning": sorted(reasoning_models),
|
| 35 |
+
"casual": sorted(casual_models)
|
| 36 |
+
}
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return {
|
| 39 |
+
"reasoning": ["openai/gpt-4-turbo"],
|
| 40 |
+
"casual": []
|
| 41 |
}
|
| 42 |
|
| 43 |
+
------------------------------------------------------------------
|
| 44 |
+
|
| 45 |
+
Format cronologia
|
| 46 |
+
|
| 47 |
+
------------------------------------------------------------------
|
| 48 |
+
|
| 49 |
+
def format_history(history: list) -> str: output = "" for msg in history: if msg["role"] == "user": output += f"\U0001f9d1\u200d\U0001f4bb Tu:\n{msg['content']}\n\n" elif msg["role"] == "assistant": output += f"\U0001f916 Assistente:\n{msg['content']}\n\n" return output.strip()
|
| 50 |
+
|
| 51 |
+
------------------------------------------------------------------
|
| 52 |
+
|
| 53 |
+
Chat
|
| 54 |
+
|
| 55 |
+
------------------------------------------------------------------
|
| 56 |
+
|
| 57 |
+
def chat_with_openrouter(prompt: str, selected_model: str, history: list): headers = { "Authorization": f"Bearer {OPENROUTER_API_KEY}", "Content-Type": "application/json" }
|
| 58 |
+
|
| 59 |
+
history.append({"role": "user", "content": prompt})
|
| 60 |
+
|
| 61 |
+
payload = {
|
| 62 |
+
"model": selected_model,
|
| 63 |
+
"messages": history,
|
| 64 |
+
"max_tokens": 4096,
|
| 65 |
+
"temperature": 0.7,
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=60)
|
| 70 |
+
resp.raise_for_status()
|
| 71 |
+
reply = resp.json()["choices"][0]["message"]["content"]
|
| 72 |
+
history.append({"role": "assistant", "content": reply})
|
| 73 |
+
return history, format_history(history)
|
| 74 |
+
except Exception as e:
|
| 75 |
+
error_msg = f"\u274c Errore: {e}"
|
| 76 |
+
history.append({"role": "assistant", "content": error_msg})
|
| 77 |
+
return history, format_history(history)
|
| 78 |
+
|
| 79 |
+
------------------------------------------------------------------
|
| 80 |
+
|
| 81 |
+
Interfaccia Gradio
|
| 82 |
+
|
| 83 |
+
------------------------------------------------------------------
|
| 84 |
+
|
| 85 |
+
def build_interface(): grouped_models = fetch_models_grouped() default_model = grouped_models["reasoning"][0] if grouped_models["reasoning"] else ""
|
| 86 |
+
|
| 87 |
+
with gr.Blocks(title="NotExistChatter β Chat con storico") as demo:
|
| 88 |
+
gr.Markdown("## \U0001f916 Project Adam β Chat dinamica con modelli open source")
|
| 89 |
+
|
| 90 |
+
saved_chats = gr.State({})
|
| 91 |
+
current_chat_name = gr.State("Chat #1")
|
| 92 |
+
|
| 93 |
+
with gr.Row():
|
| 94 |
+
with gr.Column(scale=1):
|
| 95 |
+
gr.Markdown("### \U0001f4ac Chat salvate")
|
| 96 |
+
chat_selector = gr.Radio(choices=[], label="Storico", interactive=True)
|
| 97 |
+
load_btn = gr.Button("\U0001f4c2 Carica Chat")
|
| 98 |
+
new_chat_btn = gr.Button("\U0001f195 Nuova Chat")
|
| 99 |
+
save_name = gr.Textbox(label="\U0001f4be Nome", placeholder="Es. brainstorming")
|
| 100 |
+
save_btn = gr.Button("\U0001f4be Salva Chat")
|
| 101 |
+
|
| 102 |
+
with gr.Column(scale=4):
|
| 103 |
+
with gr.Row():
|
| 104 |
+
reasoning_dropdown = gr.Dropdown(choices=grouped_models["reasoning"], label="\U0001f9e0 Modelli con Ragionamento", interactive=True)
|
| 105 |
+
casual_dropdown = gr.Dropdown(choices=grouped_models["casual"], label="\u26a1\ufe0f Modelli Generici", interactive=True)
|
| 106 |
+
|
| 107 |
+
output_box = gr.Textbox(label="Conversazione", interactive=False, lines=20, max_lines=40)
|
| 108 |
+
prompt_box = gr.Textbox(label="Prompt", placeholder="Scrivi qui il tuo messaggio...", lines=4, max_lines=10)
|
| 109 |
+
send_btn = gr.Button("Invia", variant="primary")
|
| 110 |
+
chat_history = gr.State([])
|
| 111 |
+
|
| 112 |
+
def resolve_model(prompt, reasoning_model, casual_model, history):
|
| 113 |
+
model = reasoning_model or casual_model
|
| 114 |
+
if not model:
|
| 115 |
+
history.append({"role": "assistant", "content": "\u26a0\ufe0f Seleziona almeno un modello."})
|
| 116 |
+
return history, format_history(history)
|
| 117 |
+
return chat_with_openrouter(prompt, model, history)
|
| 118 |
+
|
| 119 |
+
def reset_chat():
|
| 120 |
+
return [], "", f"Chat #{os.urandom(2).hex()}", gr.update(value=None)
|
| 121 |
+
|
| 122 |
+
def save_chat_fn(chats, name, history):
|
| 123 |
+
if not name:
|
| 124 |
+
return chats, gr.update(choices=list(chats.keys())), "\u26a0\ufe0f Inserisci un nome valido."
|
| 125 |
+
chats[name] = history.copy()
|
| 126 |
+
return chats, gr.update(choices=list(chats.keys())), f"\u2705 Chat salvata: '{name}'"
|
| 127 |
+
|
| 128 |
+
def load_chat_fn(chats, selected_name):
|
| 129 |
+
if not selected_name or selected_name not in chats:
|
| 130 |
+
return [], "", "\u26a0\ufe0f Seleziona una chat valida."
|
| 131 |
+
history = chats[selected_name]
|
| 132 |
+
return history, format_history(history), selected_name
|
| 133 |
+
|
| 134 |
+
send_btn.click(fn=resolve_model, inputs=[prompt_box, reasoning_dropdown, casual_dropdown, chat_history], outputs=[chat_history, output_box])
|
| 135 |
+
prompt_box.submit(fn=resolve_model, inputs=[prompt_box, reasoning_dropdown, casual_dropdown, chat_history], outputs=[chat_history, output_box])
|
| 136 |
+
new_chat_btn.click(fn=reset_chat, inputs=[], outputs=[chat_history, output_box, current_chat_name, chat_selector])
|
| 137 |
+
save_btn.click(fn=save_chat_fn, inputs=[saved_chats, save_name, chat_history], outputs=[saved_chats, chat_selector, output_box])
|
| 138 |
+
load_btn.click(fn=load_chat_fn, inputs=[saved_chats, chat_selector], outputs=[chat_history, output_box, current_chat_name])
|
| 139 |
+
|
| 140 |
+
return demo
|
| 141 |
+
|
| 142 |
+
------------------------------------------------------------------
|
| 143 |
+
|
| 144 |
+
Bot Telegram
|
| 145 |
+
|
| 146 |
+
------------------------------------------------------------------
|
| 147 |
+
|
| 148 |
+
telegram_chat_states = {}
|
| 149 |
+
|
| 150 |
+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): chat_id = update.effective_chat.id telegram_chat_states[chat_id] = {"history": [], "model": fetch_models_grouped()["reasoning"][0]} await update.message.reply_text("\U0001f916 Benvenuto! Scrivimi un messaggio per iniziare a chattare.")
|
| 151 |
+
|
| 152 |
+
async def setmodel(update: Update, context: ContextTypes.DEFAULT_TYPE): chat_id = update.effective_chat.id args = context.args available_models = fetch_models_grouped() all_models = available_models["reasoning"] + available_models["casual"]
|
| 153 |
+
|
| 154 |
+
if not args:
|
| 155 |
+
await update.message.reply_text("\U0001f4cc Specifica il modello. Es: /setmodel openai/gpt-4-turbo")
|
| 156 |
+
return
|
| 157 |
+
|
| 158 |
+
selected_model = args[0]
|
| 159 |
+
if selected_model not in all_models:
|
| 160 |
+
await update.message.reply_text("β Modello non valido. Scegli tra:\n" + "\n".join(all_models))
|
| 161 |
+
return
|
| 162 |
+
|
| 163 |
+
telegram_chat_states.setdefault(chat_id, {"history": [], "model": selected_model})
|
| 164 |
+
telegram_chat_states[chat_id]["model"] = selected_model
|
| 165 |
+
await update.message.reply_text(f"\u2705 Modello impostato: {selected_model}")
|
| 166 |
+
|
| 167 |
+
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): chat_id = update.effective_chat.id user_input = update.message.text
|
| 168 |
+
|
| 169 |
+
if chat_id not in telegram_chat_states:
|
| 170 |
+
telegram_chat_states[chat_id] = {"history": [], "model": fetch_models_grouped()["reasoning"][0]}
|
| 171 |
+
|
| 172 |
+
state = telegram_chat_states[chat_id]
|
| 173 |
+
history, _ = chat_with_openrouter(user_input, state["model"], state["history"])
|
| 174 |
+
reply = history[-1]["content"]
|
| 175 |
+
telegram_chat_states[chat_id]["history"] = history
|
| 176 |
+
await update.message.reply_text(reply)
|
| 177 |
+
|
| 178 |
+
def run_telegram_bot(): app = ApplicationBuilder().token(TELEGRAM_BOT_TOKEN).build() app.add_handler(CommandHandler("start", start)) app.add_handler(CommandHandler("setmodel", setmodel)) app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) print("\U0001f916 Telegram bot in esecuzione...") app.run_polling()
|
| 179 |
+
|
| 180 |
+
------------------------------------------------------------------
|
| 181 |
+
|
| 182 |
+
Avvio
|
| 183 |
+
|
| 184 |
+
------------------------------------------------------------------
|
| 185 |
+
|
| 186 |
+
if name == "main": threading.Thread(target=run_telegram_bot, daemon=True).start() build_interface().launch()
|
| 187 |
+
|