artificialguybr commited on
Commit
331ac77
·
verified ·
1 Parent(s): bb6bc94

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+
5
+ # Definir variáveis de ambiente ou substituir com sua chave de API real
6
+ API_KEY = "Sua_API_KEY_aqui"
7
+ INVOKE_URL = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/df2bee43-fb69-42b9-9ee5-f4eabbeaf3a8"
8
+
9
+ headers = {
10
+ "Authorization": f"Bearer {API_KEY}",
11
+ "accept": "text/event-stream",
12
+ "content-type": "application/json",
13
+ }
14
+
15
+ BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
16
+
17
+ def clear_chat(chat_history_state, chat_message):
18
+ chat_history_state = []
19
+ chat_message = ''
20
+ return chat_history_state, chat_message
21
+
22
+ def user(message, history):
23
+ history = history or []
24
+ history.append({"role": "user", "content": message})
25
+ return "", history
26
+
27
+ def call_api(history, max_tokens, temperature, top_p, seed=42):
28
+ payload = {
29
+ "messages": history,
30
+ "temperature": temperature,
31
+ "top_p": top_p,
32
+ "max_tokens": max_tokens,
33
+ "seed": seed,
34
+ "stream": True
35
+ }
36
+ response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
37
+ full_response = ""
38
+ for line in response.iter_lines():
39
+ if line:
40
+ decoded_line = line.decode("utf-8")
41
+ if "data:" in decoded_line:
42
+ json_data = json.loads(decoded_line.replace("data:", ""))
43
+ if "choices" in json_data and len(json_data["choices"]) > 0:
44
+ deltas = json_data["choices"][0].get("delta", {})
45
+ if "content" in deltas:
46
+ full_response += deltas["content"]
47
+ return full_response
48
+
49
+ def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
50
+ system_message_to_use = system_message if system_message.strip() else BASE_SYSTEM_MESSAGE
51
+ if history and "role" in history[-1] and history[-1]["role"] == "user":
52
+ history.append({"role": "system", "content": system_message_to_use})
53
+ assistant_response = call_api(history, max_tokens, temperature, top_p)
54
+ if assistant_response:
55
+ history.append({"role": "assistant", "content": assistant_response})
56
+ return history, "", ""
57
+
58
+ # Gradio interface setup
59
+ with gr.Blocks() as demo:
60
+ with gr.Row():
61
+ with gr.Column():
62
+ gr.Markdown("## Your Chatbot Interface")
63
+ chatbot = gr.Chatbot()
64
+ message = gr.Textbox(label="What do you want to chat about?", placeholder="Ask me anything.", lines=3)
65
+ submit = gr.Button(value="Send message")
66
+ clear = gr.Button(value="New topic")
67
+ system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)
68
+ max_tokens = gr.Slider(20, 512, label="Max Tokens", step=20, value=500)
69
+ temperature = gr.Slider(0.0, 1.0, label="Temperature", step=0.1, value=0.7)
70
+ top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.95)
71
+ chat_history_state = gr.State([])
72
+
73
+ def update_chatbot(interface, inputs, outputs):
74
+ message, chat_history = inputs
75
+ _, chat_history, _ = user(message, chat_history)
76
+ chat_history, _, _ = chat(chat_history, system_msg.value, max_tokens.value, temperature.value, top_p.value, 40, 1.1)
77
+ outputs[0].update(chat_history)
78
+ outputs[1].update(chat_history)
79
+ outputs[2].update("")
80
+
81
+ submit.click(
82
+ fn=update_chatbot,
83
+ inputs=[message, chat_history_state],
84
+ outputs=[chatbot, chat_history_state, message]
85
+ )
86
+
87
+ clear.click(
88
+ fn=lambda: clear_chat([], ""),
89
+ inputs=[chat_history_state, message],
90
+ outputs=[chat_history_state, message]
91
+ )
92
+
93
+ demo.launch()