bstraehle commited on
Commit
e8da79a
1 Parent(s): fc415e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py CHANGED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+
4
+ from openai import OpenAI
5
+
6
+ config = {
7
+ "max_tokens": 1000,
8
+ "model": "gpt-4",
9
+ "temperature": 0
10
+ }
11
+
12
+ def invoke(openai_api_key, prompt):
13
+ if (openai_api_key == ""):
14
+ raise gr.Error("OpenAI API Key is required.")
15
+ if (prompt == ""):
16
+ raise gr.Error("Prompt is required.")
17
+
18
+ os.environ["OPENAI_API_KEY"] = openai_api_key
19
+
20
+ content = ""
21
+
22
+ try:
23
+ client = OpenAI()
24
+
25
+ completion = client.chat.completions.create(
26
+ max_tokens = config["max_tokens"],
27
+ messages = [{"role": "user", "content": prompt}],
28
+ model = config["model"],
29
+ temperature = config["temperature"])
30
+
31
+ content = completion.choices[0].message.content
32
+ except Exception as e:
33
+ err_msg = e
34
+
35
+ raise gr.Error(e)
36
+
37
+ return content
38
+
39
+ description = """<a href='https://www.gradio.app/'>Gradio</a> UI using the <a href='https://openai.com/'>OpenAI</a> SDK
40
+ with <a href='https://openai.com/research/gpt-4'>gpt-4</a> model."""
41
+
42
+ gr.close_all()
43
+
44
+ demo = gr.Interface(fn = invoke,
45
+ inputs = [gr.Textbox(label = "OpenAI API Key", type = "password", lines = 1),
46
+ gr.Textbox(label = "Prompt", lines = 1)],
47
+ outputs = [gr.Textbox(label = "Completion", lines = 1)],
48
+ title = "Generative AI - LLM",
49
+ description = description)
50
+
51
+ demo.launch()