huohguohbo commited on
Commit
3d1a684
·
1 Parent(s): 540f76d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -31
app.py CHANGED
@@ -1,43 +1,37 @@
1
  import openai
2
- import re
3
  import gradio as gr
4
 
5
- # Define a function to solve math problems
6
- def solve_math_problem(prompt, api_key):
7
- # Set up the OpenAI API credentials
8
- openai.api_key = api_key
9
-
10
- # Send the prompt to OpenAI's GPT-3 engine
11
- response = openai.Completion.create(
12
- engine="davinci",
13
- prompt=prompt,
14
- max_tokens=100,
15
- n=1,
16
- stop=None,
17
- temperature=0.5,
18
- )
19
 
20
- # Extract the answer from the response
21
- answer = response.choices[0].text.strip()
22
 
23
- # Remove any non-numeric characters from the answer
24
- answer = re.sub("[^0-9\.]", "", answer)
 
 
 
 
 
 
 
 
 
 
25
 
26
- return float(answer)
27
 
28
- # Define the Gradio interface
29
  iface = gr.Interface(
30
- fn=solve_math_problem,
31
  inputs=[
32
- gr.inputs.Textbox(label="Enter a math problem"),
33
- gr.inputs.Textbox(label="Enter your OpenAI API key", type="password")
 
34
  ],
35
- outputs=gr.outputs.Textbox(label="Answer"),
36
- title="OpenAI Calculator",
37
- description="Enter a math problem and let OpenAI solve it for you!",
38
- live=True,
39
- button_text="Calculate"
40
  )
41
 
42
- # Launch the Gradio interface
43
- iface.launch()
 
1
  import openai
 
2
  import gradio as gr
3
 
4
+ def chat(api_key, message, model):
5
+ if not api_key:
6
+ return "Please enter a valid API key."
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ openai.api_key = api_key
 
9
 
10
+ try:
11
+ response = openai.Completion.create(
12
+ engine=model,
13
+ prompt=message,
14
+ max_tokens=50,
15
+ n=1,
16
+ stop=None,
17
+ temperature=0.5,
18
+ )
19
+ return response.choices[0].text.strip()
20
+ except Exception as e:
21
+ return f"Error: {str(e)}"
22
 
23
+ models = ["gpt-4", "text-davinci-002", "text-curie-002", "text-babbage-002", "text-ada-002"]
24
 
 
25
  iface = gr.Interface(
26
+ fn=chat,
27
  inputs=[
28
+ gr.inputs.Textbox(lines=1, label="API Key"),
29
+ gr.inputs.Textbox(lines=5, label="Message"),
30
+ gr.inputs.Dropdown(choices=models, label="Model"),
31
  ],
32
+ outputs=gr.outputs.Textbox(label="Response"),
33
+ title="GPT-4 Chat App",
34
+ description="A simple chat app using OpenAI GPT-4 and Gradio.",
 
 
35
  )
36
 
37
+ iface.launch()