Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,99 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
import gradio as gr
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
client = OpenAI()
|
| 6 |
+
|
| 7 |
+
taxa = {
|
| 8 |
+
"EUR": 0.93,
|
| 9 |
+
"USD": 1,
|
| 10 |
+
"BRL": 5
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
def converter_moeda(quantidade, origem, destino):
|
| 14 |
+
print("A função foi chamada")
|
| 15 |
+
origem = origem.upper()
|
| 16 |
+
destino = destino.upper()
|
| 17 |
+
|
| 18 |
+
if origem not in taxa :
|
| 19 |
+
return {"error": "Moeda não suportada: {origem}"}
|
| 20 |
+
if destino not in taxa :
|
| 21 |
+
return {"error": "Moeda não suportada {destino}"}
|
| 22 |
+
|
| 23 |
+
quantidade_dolar = quantidade / taxa[origem]
|
| 24 |
+
quantidade_convertida = quantidade_dolar * taxa[destino]
|
| 25 |
+
|
| 26 |
+
return {
|
| 27 |
+
"quantidade": quantidade,
|
| 28 |
+
"origem": origem,
|
| 29 |
+
"destino": destino,
|
| 30 |
+
"quantidade_convertida": round(quantidade_convertida, 2),
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
tools_moeda = [
|
| 34 |
+
{
|
| 35 |
+
"type": "function",
|
| 36 |
+
"name": "converter_moeda",
|
| 37 |
+
"description": "Converte uma quantia de uma moeda para outra. Aceita as moedas: EUR, USD, BRL.",
|
| 38 |
+
"parameters": {
|
| 39 |
+
"type": "object",
|
| 40 |
+
"properties": {
|
| 41 |
+
"quantidade": {
|
| 42 |
+
"type": "number",
|
| 43 |
+
"description": "A quantia a ser convertida.",
|
| 44 |
+
},
|
| 45 |
+
"origem": {
|
| 46 |
+
"type": "string",
|
| 47 |
+
"description": "A moeda de origem.",
|
| 48 |
+
},
|
| 49 |
+
"destino": {
|
| 50 |
+
"type": "string",
|
| 51 |
+
"description": "A moeda de destino.",
|
| 52 |
+
},
|
| 53 |
+
},
|
| 54 |
+
"required": ["quantidade", "origem", "destino"],
|
| 55 |
+
"additionalProperties": False
|
| 56 |
+
},
|
| 57 |
+
"strict": True,
|
| 58 |
+
}
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
def response_fn(message, history):
|
| 62 |
+
openai_history = []
|
| 63 |
+
|
| 64 |
+
openai_history.append({"role": "user", "content": message})
|
| 65 |
+
|
| 66 |
+
response = client.responses.create(
|
| 67 |
+
model= "o4-mini",
|
| 68 |
+
instructions="Responda como um pirata",
|
| 69 |
+
input=openai_history,
|
| 70 |
+
tools=tools_moeda
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# verifica se o modelo quer fazer uma chamada de função
|
| 74 |
+
if response.output and isinstance(response.output, list) and response.output[1].type == "function_call":
|
| 75 |
+
|
| 76 |
+
tool_reasoning = response.output[0]
|
| 77 |
+
tool_call = response.output[1]
|
| 78 |
+
args = json.loads(tool_call.arguments)
|
| 79 |
+
resultado_conversao = converter_moeda(**args)
|
| 80 |
+
|
| 81 |
+
openai_history.append(tool_reasoning)
|
| 82 |
+
openai_history.append(tool_call)
|
| 83 |
+
openai_history.append({
|
| 84 |
+
"type": "function_call_output",
|
| 85 |
+
"call_id": tool_call.call_id,
|
| 86 |
+
"output": json.dumps(resultado_conversao),
|
| 87 |
+
})
|
| 88 |
+
|
| 89 |
+
response = client.responses.create(
|
| 90 |
+
model= "o4-mini",
|
| 91 |
+
instructions="Responda como um pirata",
|
| 92 |
+
input=openai_history,
|
| 93 |
+
tools=tools_moeda
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
return response.output_text
|
| 97 |
+
|
| 98 |
+
gr.ChatInterface(response_fn, type="messages").launch()
|
| 99 |
+
|