Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,32 +16,27 @@ class AnalystModel:
|
|
| 16 |
"Sei un Senior Software Architect specializzato in Cybersecurity. "
|
| 17 |
"Lavora per piccoli passi: analizza un concetto alla volta e chiedi conferme. "
|
| 18 |
"Solo su richiesta esplicita, genera il 'Blueprint Finale' completo. "
|
| 19 |
-
"Analizza sempre il codice per identificare vulnerabilità
|
| 20 |
)
|
| 21 |
|
| 22 |
-
def get_response(self,
|
| 23 |
if not self.token:
|
| 24 |
-
return "Errore: Variabile
|
| 25 |
-
|
| 26 |
-
# Conversione da Formato Tuple (Gradio Legacy) a Messaggi (API)
|
| 27 |
-
messages = [{"role": "system", "content": self.system_instructions}]
|
| 28 |
-
for user_msg, bot_msg in history_tuples:
|
| 29 |
-
if user_msg:
|
| 30 |
-
messages.append({"role": "user", "content": user_msg})
|
| 31 |
-
if bot_msg:
|
| 32 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
response = self.client.chat_completion(
|
| 36 |
-
|
| 37 |
max_tokens=2500,
|
| 38 |
temperature=0.3
|
| 39 |
)
|
| 40 |
return str(response.choices[0].message.content)
|
| 41 |
except Exception as e:
|
| 42 |
-
return f"Errore
|
| 43 |
|
| 44 |
-
# --- 2. CONTROLLER (MVVM) ---
|
| 45 |
model_engine = AnalystModel()
|
| 46 |
|
| 47 |
def chat_step(user_message, history):
|
|
@@ -51,24 +46,23 @@ def chat_step(user_message, history):
|
|
| 51 |
if history is None:
|
| 52 |
history = []
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
bot_res = model_engine.get_response(payload_history)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
history.append(
|
| 63 |
|
| 64 |
return "", history
|
| 65 |
|
| 66 |
# --- 3. INTERFACCIA GRADIO ---
|
| 67 |
with gr.Blocks(title="Architect Analyst AI") as demo:
|
| 68 |
-
gr.Markdown("## 🏛️ Architect Analyst (
|
| 69 |
-
gr.Markdown("
|
| 70 |
|
| 71 |
-
#
|
| 72 |
chatbot = gr.Chatbot(label="Discussione Progetto", height=600)
|
| 73 |
|
| 74 |
with gr.Row():
|
|
@@ -99,5 +93,5 @@ if __name__ == "__main__":
|
|
| 99 |
demo.launch(
|
| 100 |
server_name="0.0.0.0",
|
| 101 |
server_port=7860,
|
| 102 |
-
theme=gr.themes.Soft()
|
| 103 |
)
|
|
|
|
| 16 |
"Sei un Senior Software Architect specializzato in Cybersecurity. "
|
| 17 |
"Lavora per piccoli passi: analizza un concetto alla volta e chiedi conferme. "
|
| 18 |
"Solo su richiesta esplicita, genera il 'Blueprint Finale' completo. "
|
| 19 |
+
"Analizza sempre il codice per identificare vulnerabilità (OWASP, Injection)."
|
| 20 |
)
|
| 21 |
|
| 22 |
+
def get_response(self, messages):
|
| 23 |
if not self.token:
|
| 24 |
+
return "Errore: Variabile 'HF_TOKEN' non configurata."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Prepariamo il payload con le istruzioni di sistema
|
| 27 |
+
payload = [{"role": "system", "content": self.system_instructions}] + messages
|
| 28 |
+
|
| 29 |
try:
|
| 30 |
response = self.client.chat_completion(
|
| 31 |
+
payload,
|
| 32 |
max_tokens=2500,
|
| 33 |
temperature=0.3
|
| 34 |
)
|
| 35 |
return str(response.choices[0].message.content)
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"Errore API: {str(e)}"
|
| 38 |
|
| 39 |
+
# --- 2. CONTROLLER (MVVM Logic) ---
|
| 40 |
model_engine = AnalystModel()
|
| 41 |
|
| 42 |
def chat_step(user_message, history):
|
|
|
|
| 46 |
if history is None:
|
| 47 |
history = []
|
| 48 |
|
| 49 |
+
# Aggiunta messaggio utente nel formato richiesto: DICTIONARY
|
| 50 |
+
history.append({"role": "user", "content": user_message})
|
| 51 |
+
|
| 52 |
+
# Otteniamo la risposta passando la history corrente (già in formato messaggi)
|
| 53 |
+
bot_res = model_engine.get_response(history)
|
|
|
|
| 54 |
|
| 55 |
+
# Aggiunta risposta assistente
|
| 56 |
+
history.append({"role": "assistant", "content": bot_res})
|
| 57 |
|
| 58 |
return "", history
|
| 59 |
|
| 60 |
# --- 3. INTERFACCIA GRADIO ---
|
| 61 |
with gr.Blocks(title="Architect Analyst AI") as demo:
|
| 62 |
+
gr.Markdown("## 🏛️ Architect Analyst (Hybrid Mode)")
|
| 63 |
+
gr.Markdown("_Analisi architetturale con validazione di sicurezza in tempo reale._")
|
| 64 |
|
| 65 |
+
# NON inseriamo type="messages" qui perché la tua versione lo rifiuta in __init__
|
| 66 |
chatbot = gr.Chatbot(label="Discussione Progetto", height=600)
|
| 67 |
|
| 68 |
with gr.Row():
|
|
|
|
| 93 |
demo.launch(
|
| 94 |
server_name="0.0.0.0",
|
| 95 |
server_port=7860,
|
| 96 |
+
theme=gr.themes.Soft()
|
| 97 |
)
|