Spaces:
Runtime error
Runtime error
File size: 3,381 Bytes
6ecfcf0 487e4c6 6ecfcf0 487e4c6 6ecfcf0 487e4c6 846f5b8 487e4c6 6ecfcf0 487e4c6 6ecfcf0 487e4c6 6ecfcf0 487e4c6 be75daa 282d234 be75daa 846f5b8 487e4c6 b92d835 9a2d3df b92d835 487e4c6 b92d835 be75daa 487e4c6 9a57779 b92d835 6ecfcf0 9a57779 b92d835 487e4c6 244bb6f 6ecfcf0 487e4c6 |
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 |
import streamlit as st
import openai
# Set Streamlit page configuration
st.set_page_config(page_title="AI Prompt Enhancer", layout="centered")
# OpenAI API Key (Store securely in Streamlit secrets)
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
# Initialize OpenAI Client
client = openai.OpenAI(api_key=OPENAI_API_KEY)
# π Dictionary of Prompting Techniques
prompt_techniques = {
"π Chain of Thought (CoT)": "Breaks down reasoning into step-by-step logic.",
"π Few-Shot Learning": "Adds relevant examples for better AI understanding.",
"π§© ReAct (Reasoning + Acting)": "Improves AI decision-making with iterative steps.",
"π² Tree of Thoughts (ToT)": "Generates multiple possible reasoning paths.",
"π‘ Self-Consistency": "Reframes the prompt to generate diverse responses.",
"π HyDE (Hypothetical Doc Embeddings)": "Structures prompts for better retrieval-based outputs.",
"π Least-to-Most Prompting": "Breaks complex tasks into smaller, manageable steps.",
"π Graph Prompting": "Optimizes structured data queries and relationships."
}
# π― Title
st.title("β¨ AI Prompt Enhancer")
# π‘ Prompt Input Box
user_prompt = st.text_area("π‘ Prompt Idea Box", placeholder="Enter your prompt here...")
# π Dropdown for Selecting a Prompting Technique
selected_technique = st.selectbox(
"π οΈ Choose a Prompting Technique",
options=list(prompt_techniques.keys()),
format_func=lambda x: f"{x} - {prompt_techniques[x]}" # Display name + description
)
# π Enhance Prompt Button
if st.button("Enhance Prompt"):
if user_prompt.strip() == "":
st.warning("β οΈ Please enter a prompt before enhancing.")
else:
try:
# Ensure max_tokens is an integer
# max_tokens = int(min(len(user_prompt) * 1.5, 500)) # Convert to integer
max_tokens = None
# Generate enhanced prompt using OpenAI API
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": f"""
Your task is to rewrite and improve the given prompt using the '{selected_technique}' technique.
**DO NOT ANSWER** the prompt.
**DO NOT GENERATE ADDITIONAL INFORMATION.**
**ONLY return an improved version of the prompt, making it clearer, more specific, and optimized for AI response generation.**
"""
},
{"role": "user", "content": user_prompt}
],
temperature=0.3, # Low randomness
max_tokens=max_tokens # Ensure integer value
)
# Ensure OpenAI returns only plain text
enhanced_prompt = completion.choices[0].message.content.strip()
# Remove markdown-style bold formatting (**text**)
import re
enhanced_prompt = re.sub(r"\*\*(.*?)\*\*", r"\1", enhanced_prompt)
# π Display Enhanced Prompt in a Larger Text Box
st.subheader("πΉ Enhanced Prompt:")
st.text_area(" ", value=enhanced_prompt, height=450 ) # Bigger text box
except Exception as e:
st.error(f"β Error: {str(e)}")
|