|
import gradio as gr |
|
import openai |
|
from dotenv import load_dotenv |
|
import os |
|
|
|
|
|
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)}" |
|
|
|
|
|
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() |