SyedHasanCronosPMC commited on
Commit
8368716
·
verified ·
1 Parent(s): 45aaebd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -31
app.py CHANGED
@@ -1,42 +1,41 @@
1
- import os
2
- import openai
3
  import gradio as gr
 
 
 
4
 
5
- # Retrieve OpenAI API key from Hugging Face Secrets
 
6
  openai_api_key = os.getenv("OPENAI_API_KEY")
 
7
 
8
- def chatbot(user_input, history=[]):
9
- if not openai_api_key:
10
- return "⚠️ API key is missing. Please configure it in Hugging Face Secrets.", history
11
-
12
- history.append({"role": "user", "content": user_input})
13
-
14
  try:
15
- response = openai.ChatCompletion.create(
16
- model="gpt-4o",
17
- messages=history,
 
 
 
 
 
18
  temperature=0.7,
19
- max_tokens=200,
20
- top_p=1,
21
- api_key=openai_api_key # Explicitly passing API key
22
  )
23
-
24
- bot_reply = response["choices"][0]["message"]["content"]
25
- history.append({"role": "assistant", "content": bot_reply})
26
-
27
  except Exception as e:
28
- bot_reply = f"Error: {str(e)}"
29
-
30
- return bot_reply, history
31
-
32
- # Gradio Interface (Removing invalid keyword arguments)
33
- chatbot_ui = gr.ChatInterface(
34
- fn=chatbot,
35
- title="AI Chatbot",
36
- description="A simple chatbot powered by GPT-4o.",
37
- theme="soft"
 
38
  )
39
 
40
- # Launch the app
41
  if __name__ == "__main__":
42
- chatbot_ui.launch()
 
 
 
1
  import gradio as gr
2
+ import openai
3
+ from dotenv import load_dotenv
4
+ import os
5
 
6
+ # Load environment variables
7
+ load_dotenv()
8
  openai_api_key = os.getenv("OPENAI_API_KEY")
9
+ client = openai.OpenAI(api_key=openai_api_key)
10
 
11
+ def get_python_help(question):
 
 
 
 
 
12
  try:
13
+ messages = [
14
+ {"role": "system", "content": "You are a helpful assistant for Python programming."},
15
+ {"role": "user", "content": question}
16
+ ]
17
+
18
+ response = client.chat.completions.create(
19
+ model="gpt-4",
20
+ messages=messages,
21
  temperature=0.7,
22
+ max_tokens=150
 
 
23
  )
24
+
25
+ return response.choices[0].message.content
 
 
26
  except Exception as e:
27
+ return f"Error: {str(e)}"
28
+
29
+ # Create Gradio interface
30
+ iface = gr.Interface(
31
+ fn=get_python_help,
32
+ inputs=gr.Textbox(label="Your Python Question:", placeholder="Type your Python question here..."),
33
+ outputs=gr.Textbox(label="Python Tutor Bot Response"),
34
+ title="Python Tutor Bot",
35
+ description="Ask your Python programming questions! Type 'exit' to end the session.",
36
+ theme="default",
37
+ examples=[["What is a tuple in Python?"], ["How do I use list comprehension?"]],
38
  )
39
 
 
40
  if __name__ == "__main__":
41
+ iface.launch()