Spaces:
Sleeping
Sleeping
changes
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
|
| 6 |
+
# API Key Input
|
| 7 |
+
api_key = os.environ['OPENAI_API_KEY']
|
| 8 |
+
client = OpenAI(
|
| 9 |
+
api_key=api_key
|
| 10 |
+
)
|
| 11 |
+
# Set up the OpenAI API client
|
| 12 |
+
openai.api_key = api_key
|
| 13 |
+
|
| 14 |
+
def generate_ai_response(prompt):
|
| 15 |
+
"""Simulates generating a response from an AI model.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
prompt: The prompt to send to the AI model.
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
response from the AI model.
|
| 22 |
+
"""
|
| 23 |
+
try:
|
| 24 |
+
completion = openai.chat.completions.create(
|
| 25 |
+
model="gpt-4o",
|
| 26 |
+
messages=[
|
| 27 |
+
{
|
| 28 |
+
"role": "developer",
|
| 29 |
+
"content": [
|
| 30 |
+
{
|
| 31 |
+
"type": "text",
|
| 32 |
+
"text": """
|
| 33 |
+
You are a programming assistant focused on providing accurate,
|
| 34 |
+
clear, and concise answers to technical questions. Your goal
|
| 35 |
+
is to help users solve programming problems efficiently, explain
|
| 36 |
+
concepts clearly, and provide examples when appropriate. Use a
|
| 37 |
+
professional yet approachable tone.
|
| 38 |
+
"""
|
| 39 |
+
}
|
| 40 |
+
]
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"role": "user",
|
| 44 |
+
"content": [
|
| 45 |
+
{
|
| 46 |
+
"type": "text",
|
| 47 |
+
"text": prompt
|
| 48 |
+
}
|
| 49 |
+
]
|
| 50 |
+
}
|
| 51 |
+
]
|
| 52 |
+
)
|
| 53 |
+
# Extract and display the response
|
| 54 |
+
model_response = completion.choices[0].message.content
|
| 55 |
+
st.success("Here's the response:")
|
| 56 |
+
return model_response
|
| 57 |
+
except Exception as e:
|
| 58 |
+
st.error(f"An error occurred: {e}")
|
| 59 |
+
return None
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# Define common programming tasks
|
| 63 |
+
programming_tasks = [
|
| 64 |
+
"Write a function to reverse a string",
|
| 65 |
+
"Create a class to represent a bank account",
|
| 66 |
+
"Implement a binary search algorithm",
|
| 67 |
+
"Write a script to scrape data from a website",
|
| 68 |
+
"Create a function to validate an email address",
|
| 69 |
+
"Implement a linked list data structure",
|
| 70 |
+
"Write a program to find the factorial of a number",
|
| 71 |
+
"Create a function to sort a list of numbers",
|
| 72 |
+
"Implement a queue data structure",
|
| 73 |
+
"Write a program to convert Celsius to Fahrenheit",
|
| 74 |
+
"Create a recursive function to calculate Fibonacci numbers",
|
| 75 |
+
"Write a function to check if a string is a palindrome",
|
| 76 |
+
"Implement a stack data structure"
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
# Streamlit app
|
| 80 |
+
st.title("AI Programming Task Assistant")
|
| 81 |
+
|
| 82 |
+
# Task selection
|
| 83 |
+
selected_task = st.selectbox("Select a programming task:", programming_tasks)
|
| 84 |
+
|
| 85 |
+
# Task details input
|
| 86 |
+
task_details = st.text_area("Provide details about the task:", height=150)
|
| 87 |
+
|
| 88 |
+
# Generate response button
|
| 89 |
+
if st.button("Get Response"):
|
| 90 |
+
if not task_details:
|
| 91 |
+
st.warning("Please provide details about the task.")
|
| 92 |
+
else:
|
| 93 |
+
# Construct the prompt
|
| 94 |
+
prompt = f"Programming Task: {selected_task}\nDetails: {task_details}"
|
| 95 |
+
|
| 96 |
+
if not api_key:
|
| 97 |
+
st.error("Please provide your OpenAI API Key.")
|
| 98 |
+
else:
|
| 99 |
+
with st.spinner("Thinking..."):
|
| 100 |
+
# Simulate AI model response
|
| 101 |
+
response = generate_ai_response(prompt)
|
| 102 |
+
st.write(response)
|