jdbolter commited on
Commit
8b87498
·
verified ·
1 Parent(s): 7bb4133

Upload mygradiobot2.py

Browse files
Files changed (1) hide show
  1. mygradiobot2.py +59 -0
mygradiobot2.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import gradio as gr
4
+ from dotenv import load_dotenv
5
+
6
+ # Load environment variables from a .env file
7
+ load_dotenv()
8
+
9
+ # Retrieve and verify the OpenAI API key from the environment variable
10
+ api_key = os.getenv('OPENAI_API_KEY')
11
+ if not api_key:
12
+ raise ValueError("OpenAI API key not found. Please set it in the environment variables.")
13
+ openai.api_key = api_key
14
+
15
+ def chatbot(input_text):
16
+ print(f"Received input: {input_text}")
17
+ try:
18
+ response = openai.chat.completions.create(
19
+ model="gpt-3.5-turbo",
20
+ messages=[
21
+ {"role": "system", "content": "You are a helpful assistant."},
22
+ {"role": "user", "content": input_text}
23
+ ]
24
+ )
25
+ ''''''
26
+ print("Response object type:", type(response))
27
+ print("Response object:", response)
28
+
29
+ print("Choices type:", type(response.choices))
30
+ print("First choice type:", type(response.choices[0]))
31
+ print("First choice:", response.choices[0])
32
+
33
+ print("Message type:", type(response.choices[0].message))
34
+ print("Message:", response.choices[0].message)
35
+
36
+ print("Content type:", type(response.choices[0].message.content))
37
+ print("Content:", response.choices[0].message.content)
38
+ ''''''
39
+ answer = response.choices[0].message.content
40
+ if isinstance(answer, str):
41
+ answer = answer.strip()
42
+
43
+ print(f"Final answer: {answer}")
44
+ return answer
45
+ except Exception as e:
46
+ print(f"Error: {e}")
47
+ return f"An error occurred: {e}"
48
+
49
+ # Create the Gradio interface
50
+ iface = gr.Interface(
51
+ fn=chatbot,
52
+ inputs="text",
53
+ outputs="text",
54
+ title="My Chatbot",
55
+ description="Ask anything!"
56
+ )
57
+
58
+ # Launch the interface
59
+ iface.launch()