Spaces:
PhilSpiel
/
Build error

PhilSpiel commited on
Commit
6f22ecb
·
verified ·
1 Parent(s): dfca332

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -13
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import os
3
  from openai import OpenAI
4
  import os.path
 
5
 
6
  ################# Start PERSONA-SPECIFIC VALUES ######################
7
  coach_code = "hi"
@@ -9,7 +10,7 @@ coach_name_short = "Hannah"
9
  coach_name_upper = "HANNAH"
10
  coach_name_long = "Hannah"
11
  sys_prompt_new = os.getenv("PROMPT_NEW")
12
- theme="ParityError/Anime"
13
  ################# End PERSONA-SPECIFIC VALUES ######################
14
 
15
  ################# Start OpenAI-SPECIFIC VALUES ######################
@@ -19,22 +20,38 @@ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
19
  openai_model = "gpt-3.5-turbo-0125"
20
  ################# End OpenAI-SPECIFIC VALUES ######################
21
 
22
- ############### CHAT ###################
 
 
 
23
  def predict(user_input, history):
24
  max_length = 500
25
- if len(user_input) > max_length:
 
 
 
 
 
 
 
 
 
 
 
 
26
  raise gr.Error(f"Input is TOO LONG. Max length is {max_length} characters. Try again.")
 
27
  history_openai_format = [
28
- {"role": "system", "content": "IDENTITY: " + sys_prompt_new}
29
- ]
30
  for human, assistant in history:
31
- history_openai_format.append({"role": "user", "content": human })
32
- history_openai_format.append({"role": "assistant", "content":assistant})
33
  history_openai_format.append({"role": "user", "content": user_input})
34
 
35
  completion = client.chat.completions.create(
36
  model=openai_model,
37
- messages= history_openai_format,
38
  temperature=1.2,
39
  frequency_penalty=0.4,
40
  presence_penalty=0.1,
@@ -42,11 +59,21 @@ def predict(user_input, history):
42
  )
43
 
44
  output_stream = ""
45
- for chunk in completion:
46
- if chunk.choices[0].delta.content is not None:
47
- output_stream = output_stream + (chunk.choices[0].delta.content)
48
- yield output_stream
49
- message_content = output_stream
 
 
 
 
 
 
 
 
 
 
50
 
51
  return message_content
52
 
 
2
  import os
3
  from openai import OpenAI
4
  import os.path
5
+ from datetime import datetime
6
 
7
  ################# Start PERSONA-SPECIFIC VALUES ######################
8
  coach_code = "hi"
 
10
  coach_name_upper = "HANNAH"
11
  coach_name_long = "Hannah"
12
  sys_prompt_new = os.getenv("PROMPT_NEW")
13
+ theme='ParityError/Anime'
14
  ################# End PERSONA-SPECIFIC VALUES ######################
15
 
16
  ################# Start OpenAI-SPECIFIC VALUES ######################
 
20
  openai_model = "gpt-3.5-turbo-0125"
21
  ################# End OpenAI-SPECIFIC VALUES ######################
22
 
23
+ tx = os.getenv("TX")
24
+ prefix = "" # "data/" if local or "/data/" if persistent in HF
25
+
26
+ ############### CHAT ###################
27
  def predict(user_input, history):
28
  max_length = 500
29
+ transcript_file_path = f"{prefix}{coach_code}-transcript.txt"
30
+ transcript = "" # Initialize the transcript variable
31
+
32
+ if user_input == tx:
33
+ try:
34
+ # Prepare the transcript for the Textbox output
35
+ if os.path.exists(transcript_file_path):
36
+ with open(transcript_file_path, "r", encoding="UTF-8") as file:
37
+ transcript = file.read()
38
+ return transcript
39
+ except FileNotFoundError:
40
+ return "File 'transcript.txt' not found."
41
+ elif len(user_input) > max_length:
42
  raise gr.Error(f"Input is TOO LONG. Max length is {max_length} characters. Try again.")
43
+
44
  history_openai_format = [
45
+ {"role": "system", "content": "IDENTITY: " + sys_prompt_new}
46
+ ]
47
  for human, assistant in history:
48
+ history_openai_format.append({"role": "user", "content": human})
49
+ history_openai_format.append({"role": "assistant", "content": assistant})
50
  history_openai_format.append({"role": "user", "content": user_input})
51
 
52
  completion = client.chat.completions.create(
53
  model=openai_model,
54
+ messages=history_openai_format,
55
  temperature=1.2,
56
  frequency_penalty=0.4,
57
  presence_penalty=0.1,
 
59
  )
60
 
61
  output_stream = ""
62
+ try:
63
+ for chunk in completion:
64
+ if chunk.choices[0].delta.content is not None:
65
+ output_stream = output_stream + (chunk.choices[0].delta.content)
66
+ message_content = output_stream
67
+ except StopAsyncIteration:
68
+ pass
69
+
70
+ # Append latest user and assistant messages to the transcript
71
+ transcript += "Date/Time: " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n\n"
72
+ transcript += f"YOU: {user_input}\n\n"
73
+ transcript += f"{coach_name_upper}: {message_content}\n\n\n"
74
+ # Write the updated transcript to the file
75
+ with open(transcript_file_path, "a", encoding="UTF-8") as file:
76
+ file.write(transcript)
77
 
78
  return message_content
79