huohguohbo commited on
Commit
5262530
·
1 Parent(s): 4395b89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -2,8 +2,8 @@ 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
 
@@ -11,31 +11,29 @@ def solve_math_problem(prompt, api_key):
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
  )
40
 
41
  # Launch the Gradio interface
 
2
  import re
3
  import gradio as gr
4
 
5
+ # Define a function to generate OpenAI responses
6
+ def generate_response(prompt, api_key):
7
  # Set up the OpenAI API credentials
8
  openai.api_key = api_key
9
 
 
11
  response = openai.Completion.create(
12
  engine="davinci",
13
  prompt=prompt,
14
+ max_tokens=150,
15
  n=1,
16
  stop=None,
17
+ temperature=0.7,
18
  )
19
 
20
+ # Extract the response from the API result
21
  answer = response.choices[0].text.strip()
22
 
23
+ return answer
 
 
 
24
 
25
  # Define the Gradio interface
26
  iface = gr.Interface(
27
+ fn=generate_response,
28
  inputs=[
29
+ gr.inputs.Textbox(label="Enter your message"),
30
  gr.inputs.Textbox(label="Enter your OpenAI API key", type="password")
31
  ],
32
+ outputs=gr.outputs.Textbox(label="OpenAI's response"),
33
+ title="OpenAI Chatbot",
34
+ description="Ask a question or say something, and OpenAI will respond!",
35
+ live=True,
36
+ button_text="Chat"
37
  )
38
 
39
  # Launch the Gradio interface