Naisong Zhou commited on
Commit
55b5946
·
1 Parent(s): 1d8e45d

add application files

Browse files
Files changed (4) hide show
  1. app.py +44 -0
  2. configs.py +1 -0
  3. requirements.txt +2 -0
  4. utils.py +59 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from utils import *
3
+ import openai
4
+
5
+ def greet(task, human_input, cooperate_style):
6
+ merged_answer = "xxx"
7
+ return f"Given task :{task},\nthe {cooperate_style} answer based of ai and human input \"{human_input}\" is:\n {merged_answer}. "
8
+
9
+ def get_answer(task, human_input, cooperate_style):
10
+ if cooperate_style=="sequential":
11
+ merged_text = merge_texts_sequential(task, human_input)
12
+ print(f"AI merged text: {merged_text}")
13
+ else:
14
+ generated_text = generate_text_with_gpt(task)
15
+ print(f"AI provided text: {generated_text}")
16
+ merged_text = merge_texts_parallel(task, human_input, generated_text)
17
+ return merged_text
18
+
19
+ def main():
20
+ with gr.Blocks() as demo:
21
+ task = gr.Textbox(label="Please Give a task description.")
22
+ human_input = gr.Textbox(label="Please provide a human answer.", value = "Write a poem about the moon.")
23
+ cooperate_style = gr.Radio(['sequential', 'parallel'], label="What type of cooperation would you like?")
24
+ greet_btn = gr.Button("Think!")
25
+ output = gr.Textbox(label="Answer with cooperation")
26
+
27
+ greet_btn.click(fn=get_answer,
28
+ inputs=[task, human_input, cooperate_style],
29
+ outputs=output,
30
+ api_name="answer")
31
+
32
+ evaluate_btn = gr.Button("Evaluate")
33
+ evaluation_results = gr.Textbox(label="Evaluation results.")
34
+ evaluate_btn.click(fn= evaluation,
35
+ inputs=[task, output],
36
+ outputs= evaluation_results,
37
+ api_name = "evaluate")
38
+ demo.launch(share=True)
39
+
40
+ if __name__ == "__main__":
41
+ main()
42
+
43
+
44
+
configs.py ADDED
@@ -0,0 +1 @@
 
 
1
+ OPEN_API_KEY = "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"
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai
2
+ gradio
utils.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ from configs import OPEN_API_KEY
3
+ def get_user_input(prompt):
4
+ return input(prompt)
5
+
6
+ def choose_cooperation_type():
7
+ choice = get_user_input("Please choose the cooperation type: \n"
8
+ +"1. Sequential: human provide an answer first and then AI provide the answer based on it.\n"
9
+ +"2. Parallel: human and AI give answers seperately and then AI does the merge.\n")
10
+ if choice == '1':
11
+ return 'sequential'
12
+ elif choice == '2':
13
+ return 'parallel'
14
+ else:
15
+ print("Invalid choice. Please try again.")
16
+ return choose_cooperation_type()
17
+
18
+ def describe_task():
19
+ task_description = get_user_input("Please describe your task: ")
20
+ if task_description.strip() == "":
21
+ # print("Task description cannot be empty. Please try again.")
22
+ # return describe_task()
23
+ task_description = "Write a poem about the moon in 3 lines."
24
+ return task_description
25
+
26
+ # def get_api_key():
27
+ # return configs.api_key # get the API key from the configs file
28
+
29
+ def generate_text_with_gpt(promts):
30
+ client = openai.OpenAI(api_key=OPEN_API_KEY)
31
+
32
+ try:
33
+ response = client.chat.completions.create(
34
+ model="gpt-3.5-turbo",
35
+ messages=[
36
+ {"role": "system", "content": promts},
37
+ ]
38
+ )
39
+ return response.choices[0].message.content
40
+ except Exception as e:
41
+ print(f"Error occured when generating texts: {e}")
42
+ return ""
43
+
44
+ def merge_texts_parallel(task_description, human_text, ai_text):
45
+ prompt = f"Given the task as :{task_description}, there are two answers provided:\n" + \
46
+ f"The first answer: {human_text}\nThe second answer: {ai_text}\n" + \
47
+ f"Merge the two answers into one in a coherent way: "
48
+ return generate_text_with_gpt(prompt)
49
+
50
+ def merge_texts_sequential(task_description, human_text):
51
+ prompt = f"Given the task as :{task_description}, the human answer is: {human_text}\n" + \
52
+ f"Provide an answer of your own but make sure it is coherent and should be based on the human answer: "
53
+ return generate_text_with_gpt(prompt)
54
+
55
+ def evaluation(task_description, text):
56
+ prompt = f"Given the task as :{task_description}, the answer provided is: {text}\n" + \
57
+ f"Evaluate the answer and provide scores between 0 and 10,\n" + \
58
+ f"where criteria for evaluation are correctness, relevance, novelty, fluency, and aesthetic Value:"
59
+ return generate_text_with_gpt(prompt)