-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
149 lines (106 loc) · 5.39 KB
/
Copy pathmain.py
File metadata and controls
149 lines (106 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
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
import sys
sys.path.append("..")
# importa tutti py
from src.router_agent import router_agent
from src.json_builder_agent import json_builder_agent
from src.ral_maker import calcola_netto
from src.plot_maker import create_waterfall
from src.schemas import UserIntent, RALInput, RALResult
from src.config import oss_model
import asyncio
import os
from enum import Enum
from dotenv import load_dotenv
from pydantic import BaseModel, Field
import json
from openai import AsyncOpenAI
from agents import Agent, Runner, trace, function_tool, OpenAIChatCompletionsModel, output_guardrail, GuardrailFunctionOutput
load_dotenv(override=True)
import gradio as gr
tax_expert = Agent(name="tax_espert", instructions="esperto nella fiscalità italiana - max 50 parole", model=oss_model)
async def chat(message, history):
fig = None # di default nessun grafico
risposta= ""
# in cima alla funzione o come variabile globale
ultimo_ral_input = None
with trace("Build json"): # ← metti un nome tuo
result = await router_agent(message)
print(f"Category: '{result.category}'")
# ROUTER If
if result.category.upper() == "INFO": # informazioni campo fiscale reddito
with trace("dai info"):
history.append({"role": "user", "content": message})
yield history, None, ""
info_result= await Runner.run(tax_expert, result.message)
history.append({"role": "assistant", "content": info_result.final_output})
yield history, None, ""
elif result.category.upper() == "DATA": # prima richiesta di calcol oral
with trace("dai info"):
history.append({"role": "user", "content": message})
yield history, None, ""
history.append({"role": "assistant", "content": "⏳ Caricamento grafico in corso..."})
yield history, None, ""
data_result=await json_builder_agent(message)
ultimo_ral_input = data_result
if data_result is None:
history.append({"role": "assistant", "content": "Non sono riuscito a elaborare i dati, riprova."})
yield history, None, ""
else:
calculation = calcola_netto(data_result)
fig = create_waterfall(calculation)
await asyncio.sleep(10)
history.append({"role": "assistant", "content": "✅ Ecco il calcolo del tuo stipendio"})
yield history, fig, ""
elif result.category.upper() == "UPDATE": # richiesta di update dati ral
with trace("dai info"):
history.append({"role": "user", "content": message})
yield history, None, ""
history.append({"role": "assistant", "content": "⏳ Caricamento grafico in corso..."})
yield history, None, ""
update_result = await json_builder_agent(f"Dati attuali: {ultimo_ral_input}. Modifica: {message}")
if update_result is None:
history.append({"role": "assistant", "content": "Non sono riuscito a elaborare i dati, riprova."})
yield history, None, ""
else:
calculation = calcola_netto(update_result)
fig = create_waterfall(calculation)
await asyncio.sleep(10)
history.append({"role": "assistant", "content": "✅ Ecco il calcolo del tuo stipendio"})
yield history, fig, ""
elif result.category.upper() == "OFF_TOPIC": # chiede informazioni non inerenti
history.append({"role": "user", "content": message})
yield history, None, ""
history.append({"role": "assistant", "content": "Argomento non inerente allo scopo del calcolatore"})
yield history, None, ""
elif result.category.upper() == "NOT_CAPABLE": # chiede feature non disponibili dallo strumento
history.append({"role": "user", "content": message})
yield history, None, ""
history.append({"role": "assistant", "content": "Feature del calcolatore non disponibile puoi inviare il suggerimento alla mail tizio@caio.it"})
yield history, None, ""
yield history, fig, ""
with gr.Blocks(
theme=gr.themes.Ocean(
primary_hue="green",
secondary_hue="emerald",
neutral_hue="gray",
),
) as app:
gr.HTML("""
<div style="background: linear-gradient(135deg, #16A34A 0%, #059669 50%, #0D9488 100%);
padding: 2rem 2rem 1.5rem 2rem; border-radius: 14px; margin-bottom: 1rem;
box-shadow: 0 4px 20px rgba(22, 163, 74, 0.25);">
<h1 style="color: white; font-size: 2.2rem; font-weight: 800; margin: 0 0 0.3rem 0;">💰 RALyzer</h1>
<p style="color: rgba(255, 255, 255, 0.9); font-size: 1.05rem; margin: 0;">Da RAL lorda a netto in tasca. Chiedi tutto su IRPEF, INPS e detrazioni.💸</p>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
chatbot = gr.Chatbot(height=480, label="🤖 AI")
msg = gr.Textbox(
placeholder="Es: RAL 35k, cos'è l'IRPEF?...",
show_label=False,
)
with gr.Column(scale=2):
plot = gr.Plot(label="📊 Waterfall")
msg.submit(fn=chat, inputs=[msg, chatbot], outputs=[chatbot, plot, msg])
app.launch(share=True)