File size: 1,237 Bytes
997502a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import gradio as gr
import openai
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
client = openai.OpenAI(api_key=openai_api_key)

def get_python_help(question):
    try:
        messages = [
            {"role": "system", "content": "You are a helpful assistant for Python programming."},
            {"role": "user", "content": question}
        ]

        response = client.chat.completions.create(
            model="gpt-4",
            messages=messages,
            temperature=0.7,
            max_tokens=150
        )
        
        return response.choices[0].message.content
    except Exception as e:
        return f"Error: {str(e)}"

# Create Gradio interface
iface = gr.Interface(
    fn=get_python_help,
    inputs=gr.Textbox(label="Your Python Question:", placeholder="Type your Python question here..."),
    outputs=gr.Textbox(label="Python Tutor Bot Response"),
    title="Python Tutor Bot",
    description="Ask your Python programming questions! Type 'exit' to end the session.",
    theme="default",
    examples=[["What is a tuple in Python?"], ["How do I use list comprehension?"]],
)

if __name__ == "__main__":
    iface.launch()