giseldo commited on
Commit
e1d7d3a
·
verified ·
1 Parent(s): b0198f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -63
app.py CHANGED
@@ -1,64 +1,99 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+