Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
|
@@ -2,61 +2,61 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import AzureOpenAI
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 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 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
messages.append({"role": "assistant", "content":
|
| 32 |
-
# Add the new user message
|
| 33 |
messages.append({"role": "user", "content": message})
|
| 34 |
|
| 35 |
-
# Call
|
| 36 |
response = client.chat.completions.create(
|
| 37 |
-
model="gpt-4o",
|
| 38 |
messages=messages
|
| 39 |
)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
chat_history
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
| 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)))
|