Alex Stern commited on
Commit
60d4ea5
·
1 Parent(s): 89e0a45

Upload 5 files

Browse files
Files changed (5) hide show
  1. .env +1 -0
  2. .gitignore +1 -0
  3. app.py +114 -0
  4. requirements.txt +3 -0
  5. script.sh +6 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_API_KEY=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
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import gradio as gr
3
+ from math import floor, log10
4
+ import dotenv
5
+ dotenv.load_dotenv()
6
+ import os
7
+
8
+ # Initialize OpenAI API
9
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
10
+
11
+ def generate_together_tv_email_response(email_content, bullet_points, tone, model_choice):
12
+ """
13
+ Generate a coherent email response based on user-provided bullet points and optional tone,
14
+ incorporating TogetherTV's values and contextual awareness.
15
+ """
16
+
17
+ # Analyze email content and prepare the prompt
18
+ tone_statement = f"\n\nTone: {tone}" if tone else ""
19
+ model_choice = model_choice if model_choice else "gpt-3.5-turbo"
20
+
21
+ prompt = (
22
+ f"Email context: {email_content}\n\n"
23
+ f"Bullet Points: {bullet_points}{tone_statement}\n\n"
24
+ "Draft a polite and professional email response, "
25
+ "aligned with TogetherTV's values, ending with a signature from the TogetherTV team."
26
+ )
27
+
28
+ # Use OpenAI API to generate email response
29
+ try:
30
+ response = client.chat.completions.create(
31
+ model=model_choice,#"gpt-3.5-turbo",#"gpt-4",
32
+ messages=[
33
+ {
34
+ "role": "system",
35
+ "content": (
36
+ "This is an email response generation task. You will be provided with an email context, "
37
+ "specific bullet points summarizing the user's query or feedback, and the desired tone of the response. "
38
+ "Your task is to draft a coherent and professional email response that addresses the bullet points, "
39
+ "aligns with the tone specified, and reflects the ethos and values of TogetherTV, a TV channel committed to social change and community engagement. "
40
+ "End the response with a polite closing and a signature from the TogetherTV team."
41
+ )
42
+ },
43
+ {
44
+ "role": "user",
45
+ "content": prompt
46
+ }
47
+ ],
48
+ temperature=0.7,
49
+ max_tokens=500,
50
+ top_p=1
51
+ )
52
+ complete_response = response.choices[0].message.content
53
+
54
+ # Calculate token usage price
55
+ cost = calculate_token_usage_price(response, model_choice)
56
+
57
+ # Return response content and cost
58
+ return complete_response, cost
59
+
60
+ except Exception as e:
61
+ print(f"An error occurred: {e}")
62
+ return str(e)
63
+
64
+ def sig_figs(x: float, precision: int):
65
+ """
66
+ Rounds a number to number of significant figures
67
+ Parameters:
68
+ - x - the number to be rounded
69
+ - precision (integer) - the number of significant figures
70
+ Returns:
71
+ - float
72
+ """
73
+
74
+ x = float(x)
75
+ precision = int(precision)
76
+
77
+ return round(x, -int(floor(log10(abs(x)))) + (precision - 1))
78
+
79
+ def calculate_token_usage_price(response, model_choice):
80
+ tokens_input=response.usage.prompt_tokens
81
+ tokens_output=response.usage.prompt_tokens
82
+
83
+ if model_choice == "gpt-3.5-turbo":
84
+ rate_input = 0.0010 / 1000
85
+ rate_output = 0.0020 / 1000
86
+ else:
87
+ rate_input = 0.03 / 1000
88
+ rate_output = 0.06 / 1000
89
+
90
+ cost = tokens_input * rate_input + tokens_output * rate_output
91
+ return sig_figs(cost,2)
92
+
93
+
94
+ def gradio_interface():
95
+ interface = gr.Interface(
96
+ fn=generate_together_tv_email_response,
97
+ inputs=[
98
+ "textarea", # Email content
99
+ "textarea", # Bullet Points
100
+ "text", # Tone
101
+ gr.Dropdown(["gpt-3.5-turbo", "gpt-4"], label="Choose Model")
102
+ ],
103
+ #outputs=["textarea","number"],
104
+ outputs=[
105
+ gr.components.Textbox(label="Email Response"),
106
+ gr.components.Textbox(label="Cost Estimate ($)")
107
+ ],
108
+ title="AI Email Response Generator",
109
+ description="Generate email responses using AI. Choose a model, then input the email context, bullet points, and optional tone for the response."
110
+ )
111
+ interface.launch(share=True)
112
+
113
+ if __name__ == "__main__":
114
+ gradio_interface()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ openai
2
+ gradio
3
+ python-dotenv
script.sh ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ git init
2
+ git add .
3
+ git commit -m "first commit"
4
+ git branch -M main
5
+ git remote add origin https://github.com/alexkstern/ttvemaildrafter.git
6
+ git push -u origin main