File size: 1,357 Bytes
92085ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
import os
import openai
import gradio as gr
from dotenv import load_dotenv

load_dotenv()
key = os.getenv("OPENAI_API_KEY")

if not key:
    raise ValueError()

openai.api_key = key

def tutor(input):
    if not input.strip():
        return "Valid only"
    
    response = openai.ChatCompletion.create(
        model= "gpt-4o-mini",
        messages =[
            {
                "role" : "system",
                "content" : "You are a PhD level Python Professor who answers questions about Python even a middle schooler can understand."
                "WHen answering, you give atleast two code examples along with it. Showing the output with the code is a must. you should tell the Exceptions and errors that may occur "
                "while coding pertaining to the question. "
            }
            { 
                "role" : "user",
                "content" : input  
            }
        ],
        temperature = 0.03,
        max_tokens=2000,
        top_p=0.1,
        frequency_penalty=0.1,
        presence_penalty=0.95
    )

    return response ["choice"][0]["message"]["content"]

chatbot_ui = gr.Interface(
    fn = tutor,
    inputs= gr.Textbox(lines=3, placeholder = "Ask"),
    outputs= gr.Textbox(),
    title="Python Tutor Bot",
    description ="Ask away"
)

if __name__ == "__main__":
     chatbot_ui.launch(share=True)