jmesplana commited on
Commit
673fa29
·
1 Parent(s): a6802de

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import time
3
+ import config
4
+ import gradio as gr
5
+
6
+ from openai import OpenAI
7
+
8
+ client = OpenAI(api_key=config.OPENAI_API_KEY)
9
+
10
+ # Initialize the client
11
+ # Set your OpenAI API key
12
+
13
+ '''file = client.files.create(
14
+ file=open("songs.txt", "rb"),
15
+ purpose='assistants'
16
+ )'''
17
+
18
+ # Step 1: Create an Assistant
19
+ assistant = client.beta.assistants.create(
20
+ name="SAFe Specialist",
21
+ instructions="As a Scaled Agile Framework (SAFe) Specialist, you guide organizations from a siloed project model \
22
+ to an integrated product mode, focusing on humanitarian organizations offering digital solutions. You understand \
23
+ their unique context by asking about current practices and challenges, using direct questions and multiple-choice \
24
+ options. You tailor responses for a smooth transition, emphasizing core SAFe principles and addressing challenges \
25
+ like cultural resistance and highlighting best practices. You communicate with a balance of honesty and \
26
+ encouragement, providing realistic yet optimistic guidance. You avoid giving advice on areas outside \
27
+ the scope of SAFe, like financial management or legal compliance. Additionally, you recognize that \
28
+ SAFe may not suit all organizations, especially those with rigid structures or those not ready for\
29
+ significant cultural changes. In such cases, you guide users towards understanding the \
30
+ limitations of SAFe in their specific context. Walk the user through the step by step process \
31
+ and ask clarifying questions one at a time and wait for an answer before responding. Then \
32
+ formulate your response.",
33
+ model="gpt-4-1106-preview",
34
+ # file_ids=[file.id],
35
+ tools=[{"type": "retrieval"}]
36
+ )
37
+
38
+ # Step 2: Create a Thread
39
+ thread = client.beta.threads.create()
40
+
41
+ def main(query, history):
42
+ # Step 3: Add a Message to a Thread
43
+ history=history,
44
+ message = client.beta.threads.messages.create(
45
+ thread_id=thread.id,
46
+ role="user",
47
+ content=query
48
+ )
49
+
50
+ # Step 4: Run the Assistant
51
+ run = client.beta.threads.runs.create(
52
+ thread_id=thread.id,
53
+ assistant_id=assistant.id,
54
+ instructions="The user is a humanitarian worker who is going through digital transformation"
55
+ )
56
+
57
+ while True:
58
+ # Wait for 5 seconds
59
+ time.sleep(0.5)
60
+
61
+ # Retrieve the run status
62
+ run_status = client.beta.threads.runs.retrieve(
63
+ thread_id=thread.id,
64
+ run_id=run.id
65
+ )
66
+
67
+ # If run is completed, get messages
68
+ if run_status.status == 'completed':
69
+ messages = client.beta.threads.messages.list(
70
+ thread_id=thread.id
71
+ )
72
+ response = ""
73
+
74
+ data = messages.data
75
+ first_thread_message = data[0]
76
+ content = first_thread_message.content
77
+ response = content[0].text.value
78
+ return response
79
+ else:
80
+ continue
81
+
82
+ # Create a Gradio Interface
83
+
84
+ iface = gr.ChatInterface(main, title="SAFe Specialist",\
85
+ description="SAFe Specialist guiding transitions with realistic and \
86
+ optimistic advice towards a product centric approach",\
87
+ examples=["How can I shift from project to product mode?",\
88
+ "What are the key SAFe principles for my organization?",\
89
+ "Can you provide options for agile practices in my setting?",\
90
+ "How do I deal with cultural resistance in SAFe adoption?", \
91
+ "What's your advice for an org with many different digital solutions?",\
92
+ "Could you walk me through the step-by-step process of moving into SAFe?"]).queue()
93
+
94
+ if __name__ == "__main__":
95
+ iface.launch()