Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import gradio as gr
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
key = os.getenv("OPENAI_API_KEY")
|
8 |
+
|
9 |
+
if not key:
|
10 |
+
raise ValueError()
|
11 |
+
|
12 |
+
openai.api_key = key
|
13 |
+
|
14 |
+
def tutor(input):
|
15 |
+
if not input.strip():
|
16 |
+
return "Valid only"
|
17 |
+
|
18 |
+
response = openai.ChatCompletion.create(
|
19 |
+
model= "gpt-4o-mini",
|
20 |
+
messages =[
|
21 |
+
{
|
22 |
+
"role" : "system",
|
23 |
+
"content" : "You are a PhD level Python Professor who answers questions about Python even a middle schooler can understand."
|
24 |
+
"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 "
|
25 |
+
"while coding pertaining to the question. "
|
26 |
+
}
|
27 |
+
{
|
28 |
+
"role" : "user",
|
29 |
+
"content" : input
|
30 |
+
}
|
31 |
+
],
|
32 |
+
temperature = 0.03,
|
33 |
+
max_tokens=2000,
|
34 |
+
top_p=0.1,
|
35 |
+
frequency_penalty=0.1,
|
36 |
+
presence_penalty=0.95
|
37 |
+
)
|
38 |
+
|
39 |
+
return response ["choice"][0]["message"]["content"]
|
40 |
+
|
41 |
+
chatbot_ui = gr.Interface(
|
42 |
+
fn = tutor,
|
43 |
+
inputs= gr.Textbox(lines=3, placeholder = "Ask"),
|
44 |
+
outputs= gr.Textbox(),
|
45 |
+
title="Python Tutor Bot",
|
46 |
+
description ="Ask away"
|
47 |
+
)
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
chatbot_ui.launch(share=True)
|