bstraehle commited on
Commit
dfa45b1
·
1 Parent(s): 6d01c05

Update app.py

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