Poojashetty357 commited on
Commit
5ca1fce
Β·
verified Β·
1 Parent(s): 9732b5e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+ from dotenv import load_dotenv
5
+
6
+ # πŸ“Œ Load environment variables from .env
7
+ load_dotenv()
8
+ api_key = os.getenv("OPENAI_API_KEY")
9
+
10
+ # πŸ“Œ Initialize OpenAI client
11
+ client = OpenAI(api_key=api_key)
12
+
13
+ # πŸ“Œ Define the Python Tutor function
14
+ def python_tutor(user_input):
15
+ response = client.chat.completions.create(
16
+ model="gpt-4.1-mini",
17
+ messages=[
18
+ {
19
+ "role": "system",
20
+ "content": [
21
+ {
22
+ "type": "text",
23
+ "text": """Answer users' Python-related questions as a tutor by providing concise explanations, illustrative examples, and sample code. Politely decline if the query is not related to Python. Encourage learning and experimentation through motivation.
24
+
25
+ - Prioritize short and clear answers on the first attempt.
26
+ - If asked again, provide a more detailed response including deeper insights into the topic.
27
+ - Always motivate users to explore further and experiment with the code.
28
+
29
+ # Steps
30
+
31
+ 1. Confirm if the question is Python-related. If not, politely inform the user and refrain from answering.
32
+ 2. Provide a concise initial answer:
33
+ - Include a brief explanation.
34
+ - Provide a straightforward code example.
35
+ 3. If the user asks for more details, expand upon the initial response:
36
+ - Elaborate on the concepts.
37
+ - Provide more comprehensive code examples.
38
+ - Discuss additional related topics if relevant.
39
+ 4. Encourage the user to experiment and learn independently.
40
+ """
41
+ }
42
+ ]
43
+ },
44
+ {"role": "user", "content": user_input}
45
+ ],
46
+ temperature=0.1,
47
+ max_tokens=1971,
48
+ stop=["bye", "done", "good bye"],
49
+ top_p=0.05,
50
+ frequency_penalty=0.07,
51
+ presence_penalty=0.11
52
+ )
53
+ return response.choices[0].message.content
54
+
55
+ # πŸ“Œ Gradio UI
56
+ gr.Interface(
57
+ fn=python_tutor,
58
+ inputs=gr.Textbox(lines=3, label="Ask your Python-related question:"),
59
+ outputs=gr.Textbox(label="Python Tutor's Answer"),
60
+ title="🐍 Python Tutor Bot",
61
+ description="Ask any Python question and get helpful explanations with examples."
62
+ ).launch()