Javierquin commited on
Commit
c65610e
·
verified ·
1 Parent(s): 1e93383
Files changed (1) hide show
  1. app.py +51 -51
app.py CHANGED
@@ -2,61 +2,61 @@ import os
2
  import gradio as gr
3
  from openai import AzureOpenAI
4
 
5
- client = AzureOpenAI(
6
- api_key=os.getenv("AZURE_KEY"),
7
- api_version=os.getenv("AZURE_VERSION"),
8
- azure_endpoint=os.getenv("AZURE_URL")
9
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- def chat_with_gpt4o(message, chat_history, company_data, objective, other):
12
- chat_history = chat_history or []
13
-
14
- # If this is the first message, add the initial greeting
15
- if not chat_history:
16
- initial_greeting = "Hola soy el asistente de Horizon, en que te puedo ayudar?"
17
- chat_history.append(("", initial_greeting))
18
- return "", chat_history
19
-
20
- # Create a detailed system prompt with the additional information
21
- system_prompt = f"""You are a helpful assistant. Here is some context about the conversation:
22
- Company Data: {company_data}
23
- Objective: {objective}
24
- Other Information: {other}"""
25
-
26
- # Initialize messages with the enhanced system prompt
27
  messages = [{"role": "system", "content": system_prompt}]
28
- # Append previous conversation
29
- for user, assistant in chat_history:
30
- messages.append({"role": "user", "content": user})
31
- messages.append({"role": "assistant", "content": assistant})
32
- # Add the new user message
33
  messages.append({"role": "user", "content": message})
34
 
35
- # Call the Azure OpenAI API
36
  response = client.chat.completions.create(
37
- model="gpt-4o", # Usa el nombre del deployment
38
  messages=messages
39
  )
40
- assistant_message = response.choices[0].message.content
41
-
42
- # Update chat history
43
- chat_history.append((message, assistant_message))
44
- # Clear the input box and return updated history
45
- return "", chat_history
46
-
47
- with gr.Blocks() as demo:
48
- with gr.Row():
49
- company_data = gr.Textbox(label="Company Data", placeholder="Enter company information...")
50
- objective = gr.Textbox(label="Objective", placeholder="Enter the objective...")
51
- other = gr.Textbox(label="Other", placeholder="Enter any other relevant information...")
52
-
53
- with gr.Row():
54
- with gr.Column(scale=2): # Chat section (2/3)
55
- chatbot = gr.Chatbot(label="GPT-4o Chat", value=[("", "Hola soy el asistente de Horizon, en que te puedo ayudar?")])
56
- msg = gr.Textbox(placeholder="Type your message here...", show_label=False)
57
- msg.submit(chat_with_gpt4o, [msg, chatbot, company_data, objective, other], [msg, chatbot])
58
-
59
- with gr.Column(scale=1): # Reasoning section (1/3)
60
- reasoning = gr.Textbox(label="Reasoning", lines=20, interactive=False)
61
-
62
- demo.launch()
 
 
2
  import gradio as gr
3
  from openai import AzureOpenAI
4
 
5
+ # Initialize Azure OpenAI client
6
+ def get_client():
7
+ return AzureOpenAI(
8
+ api_key=os.getenv("AZURE_KEY"),
9
+ api_version=os.getenv("AZURE_VERSION"),
10
+ azure_endpoint=os.getenv("AZURE_URL")
11
+ )
12
+
13
+ # Chat prediction function
14
+ def predict(message, chat_history, company_data, objective, other):
15
+ client = get_client()
16
+ chat_history = chat_history or [("", "Hola soy el asistente de Horizon, ¿en qué te puedo ayudar?")]
17
+
18
+ # Build system prompt with context
19
+ system_prompt = (
20
+ f"You are a helpful assistant. Here is some context about the conversation:\n"
21
+ f"Company Data: {company_data}\n"
22
+ f"Objective: {objective}\n"
23
+ f"Other Information: {other}"
24
+ )
25
 
26
+ # Assemble messages
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  messages = [{"role": "system", "content": system_prompt}]
28
+ for user_msg, bot_msg in chat_history:
29
+ if user_msg:
30
+ messages.append({"role": "user", "content": user_msg})
31
+ messages.append({"role": "assistant", "content": bot_msg})
 
32
  messages.append({"role": "user", "content": message})
33
 
34
+ # Call Azure OpenAI
35
  response = client.chat.completions.create(
36
+ model="gpt-4o",
37
  messages=messages
38
  )
39
+ bot_reply = response.choices[0].message.content
40
+
41
+ chat_history.append((message, bot_reply))
42
+ return chat_history, chat_history
43
+
44
+ # Gradio Interface definition
45
+ app = gr.Interface(
46
+ fn=predict,
47
+ inputs=[
48
+ gr.Textbox(label="Your Message", placeholder="Type your message here..."),
49
+ gr.State(),
50
+ gr.Textbox(label="Company Data", placeholder="Enter company information..."),
51
+ gr.Textbox(label="Objective", placeholder="Enter the objective..."),
52
+ gr.Textbox(label="Other", placeholder="Enter any other relevant information...")
53
+ ],
54
+ outputs=[
55
+ gr.Chatbot(label="GPT-4o Chat"),
56
+ gr.State()
57
+ ],
58
+ title="Horizon GPT-4o Chat",
59
+ )
60
+
61
+ if __name__ == "__main__":
62
+ app.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))