File size: 3,374 Bytes
0e813f9
 
 
 
 
 
a45a731
0e813f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b91995
0e813f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import streamlit as st
import openai
import os
from openai import OpenAI

# API Key Input
api_key = os.environ['OPENAI_API_KEY']
client = OpenAI(
  api_key=api_key
)
# Set up the OpenAI API client
openai.api_key = api_key

def generate_ai_response(prompt):
    """Simulates generating a response from an AI model.

    Args:
    prompt: The prompt to send to the AI model.

    Returns:
    response from the AI model.
    """
    try:
        completion = openai.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[
                {
                    "role": "developer",
                    "content": [
                        {
                            "type": "text",
                            "text": """
                                You are a programming assistant focused on providing accurate, 
                                clear, and concise answers to technical questions. Your goal 
                                is to help users solve programming problems efficiently, explain 
                                concepts clearly, and provide examples when appropriate. Use a 
                                professional yet approachable tone.
                            """
                        }
                    ]
                },
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text": prompt
                        }
                    ]
                }
            ]
        )
        # Extract and display the response
        model_response = completion.choices[0].message.content
        st.success("Here's the response:")
        return model_response
    except Exception as e:
        st.error(f"An error occurred: {e}")
        return None


# Define common programming tasks
programming_tasks = [
    "Write a function to reverse a string",
    "Create a class to represent a bank account",
    "Implement a binary search algorithm",
    "Write a script to scrape data from a website",
    "Create a function to validate an email address",
    "Implement a linked list data structure",
    "Write a program to find the factorial of a number",
    "Create a function to sort a list of numbers",
    "Implement a queue data structure",
    "Write a program to convert Celsius to Fahrenheit",
    "Create a recursive function to calculate Fibonacci numbers",
    "Write a function to check if a string is a palindrome",
    "Implement a stack data structure"
]

# Streamlit app
st.title("AI Programming Task Assistant")

# Task selection
selected_task = st.selectbox("Select a programming task:", programming_tasks)

# Task details input
task_details = st.text_area("Provide details about the task:", height=150)

# Generate response button
if st.button("Get Response"):
    if not task_details:
        st.warning("Please provide details about the task.")
    else:
        # Construct the prompt
        prompt = f"Programming Task: {selected_task}\nDetails: {task_details}"

        if not api_key:
            st.error("Please provide your OpenAI API Key.")
        else:
            with st.spinner("Thinking..."):
                # Simulate AI model response
                response = generate_ai_response(prompt)
                st.write(response)