Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
import gradio as gr
|
4 |
+
import pandas as pd
|
5 |
+
import os
|
6 |
+
import openai
|
7 |
+
|
8 |
+
openai.api_key = os.environ.get('GPT_3_Token')
|
9 |
+
|
10 |
+
def openai_query(
|
11 |
+
recipient:str = "Employer",
|
12 |
+
len:int = 400,
|
13 |
+
recipient_name:str = "John Doe",
|
14 |
+
context:str = "",
|
15 |
+
input:str = "",
|
16 |
+
random_state:float = 0.85
|
17 |
+
) -> str:
|
18 |
+
|
19 |
+
return openai.Completion.create(
|
20 |
+
engine='text-davinci-002',
|
21 |
+
prompt="Write a professional email to my " + recipient + " starting with Hello " + recipient_name + ", about the subject " + context + " and the email should be based on this draft: " + input,
|
22 |
+
temperature = random_state,
|
23 |
+
max_tokens= len,
|
24 |
+
frequency_penalty=0.25,
|
25 |
+
presence_penalty=0.75,
|
26 |
+
best_of=1
|
27 |
+
).get("choices")[0]['text'].strip()
|
28 |
+
|
29 |
+
|
30 |
+
def query(payload, API_URL):
|
31 |
+
print()
|
32 |
+
response = requests.request("POST", API_URL, json=payload)
|
33 |
+
return response.json()
|
34 |
+
|
35 |
+
|
36 |
+
def pre_query(model_id, context, input, dates, sender, recipient, recipient_name):
|
37 |
+
API_URL = "https://api-inference.huggingface.co/models/" + model_id
|
38 |
+
|
39 |
+
if model_id == "bigscience/T0pp":
|
40 |
+
input_string = "Write a professional email to my " + recipient + " starting with Hello " + recipient_name + ", about the subject " + context + " and the email should be based on this draft: " + input
|
41 |
+
data = query(input_string, API_URL)
|
42 |
+
if type(data) is dict:
|
43 |
+
return data['error']
|
44 |
+
else:
|
45 |
+
return data[0]['generated_text']
|
46 |
+
|
47 |
+
if model_id == "bigscience/bloom":
|
48 |
+
input_string = "Write a professional email to my " + recipient + " starting with Hello " + recipient_name + ", about the subject " + context + " and the email should be based on this draft: " + input + ": Hello " + recipient_name + ",\n\n"
|
49 |
+
data = query({
|
50 |
+
"inputs":input_string,
|
51 |
+
"parameters":{"max_new_tokens":96,
|
52 |
+
"return_full_text": False}
|
53 |
+
}, API_URL)
|
54 |
+
if type(data) is dict:
|
55 |
+
return data['error']
|
56 |
+
else:
|
57 |
+
return "Hello " + recipient_name + ",\n\n" + data[0]['generated_text'].replace(input_string,'')
|
58 |
+
|
59 |
+
if model_id == "EleutherAI/gpt-neox-20b":
|
60 |
+
input_string = "Write a professional email to my " + recipient + " starting with Hello " + recipient_name + ", about the subject " + context + " and the email should be based on this draft: " + input
|
61 |
+
data = query(input_string, API_URL)
|
62 |
+
|
63 |
+
if type(data) is dict:
|
64 |
+
return data['error']
|
65 |
+
else:
|
66 |
+
return data[0]['generated_text']
|
67 |
+
|
68 |
+
if model_id == "GPT-3":
|
69 |
+
return openai_query(recipient, 250, recipient_name, context, input)
|
70 |
+
|
71 |
+
return
|
72 |
+
|
73 |
+
title = "Email Assistant"
|
74 |
+
|
75 |
+
interface = gr.Interface(
|
76 |
+
fn = pre_query,
|
77 |
+
|
78 |
+
inputs=[gr.Dropdown(["GPT-3", "bigscience/T0pp", "bigscience/bloom", "EleutherAI/gpt-neox-20b"] ,label = "model_id"),
|
79 |
+
gr.Dropdown([ "Requesting a meeting", "Conflict with scheduled meeting time", "Requesting clarification", "Requesting to leave early", "Requesting a leave of absence", "Requesting a letter of recommendation", "Requesting a referral for a job application"], label= "Subject/Context"),
|
80 |
+
gr.Textbox(label="Input", lines=10, placeholder="Enter your Message Here!"),
|
81 |
+
gr.Textbox(label="Relevant Dates", placeholder ="MM/DD/YYYY"),
|
82 |
+
gr.Dropdown(["student", "employee", "applicant", "recruiter", "boss"], label="Sender"),
|
83 |
+
gr.Dropdown(["professor", "supervisor", "coworker", "recruiter", "boss"], label="Recipient"),
|
84 |
+
gr.Textbox(label="Recipient Name", placeholder = "George")],
|
85 |
+
|
86 |
+
outputs=[gr.Textbox(lines=10, label = "Result")],
|
87 |
+
|
88 |
+
title = title,
|
89 |
+
).launch(debug=True)
|