Spaces:
Sleeping
Sleeping
another changes
Browse files- .streamlit/secrets.toml +1 -0
- app.py +32 -126
.streamlit/secrets.toml
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
OPENAI_API_KEY = "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"
|
app.py
CHANGED
|
@@ -2,143 +2,49 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
import openai
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
# -----------------------------------------------------------------------------
|
| 8 |
-
# For security, it is recommended to set your API key as an environment variable
|
| 9 |
-
# or use Streamlit's secrets management. For example, in Streamlit Cloud, you can
|
| 10 |
-
# add openai.api_key in your secrets.toml.
|
| 11 |
-
openai.api_key = os.getenv("OPENAI_API_KEY", "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")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
# -----------------------------------------------------------------------------
|
| 16 |
-
def generate_prompt(topic, output_format, tone, length, creativity, creative_mode, fact_checking):
|
| 17 |
-
"""
|
| 18 |
-
Dynamically constructs the prompt based on the user's inputs.
|
| 19 |
-
"""
|
| 20 |
-
# Start with the basic topic text
|
| 21 |
-
prompt = f"Topic: {topic}\n\n"
|
| 22 |
-
|
| 23 |
-
# Specify the desired output format and tone
|
| 24 |
-
prompt += f"Please generate a {output_format} with a {tone} tone.\n"
|
| 25 |
-
|
| 26 |
-
# Include any additional instructions based on options
|
| 27 |
-
if creative_mode:
|
| 28 |
-
prompt += "Use creative language and storytelling techniques.\n"
|
| 29 |
-
if fact_checking:
|
| 30 |
-
prompt += "Ensure the facts mentioned are accurate.\n"
|
| 31 |
-
|
| 32 |
-
# Add instructions for length and creativity level
|
| 33 |
-
prompt += f"The text should be approximately {length} words long.\n"
|
| 34 |
-
prompt += f"Please adjust the creativity level to {creativity} (scale 1-10).\n"
|
| 35 |
-
|
| 36 |
-
return prompt
|
| 37 |
-
|
| 38 |
-
def call_openai_api(prompt, num_responses):
|
| 39 |
-
"""
|
| 40 |
-
Calls the OpenAI API with the constructed prompt and returns generated responses.
|
| 41 |
-
"""
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
temperature=0.7, # Adjust creativity level (temperature)
|
| 51 |
-
top_p=1,
|
| 52 |
-
n=1,
|
| 53 |
-
stop=None,
|
| 54 |
-
)
|
| 55 |
-
generated_text = response.choices[0].text.strip()
|
| 56 |
-
responses.append(generated_text)
|
| 57 |
-
return responses
|
| 58 |
-
|
| 59 |
except Exception as e:
|
| 60 |
-
|
| 61 |
-
return None
|
| 62 |
-
|
| 63 |
-
# -----------------------------------------------------------------------------
|
| 64 |
-
# Streamlit User Interface
|
| 65 |
-
# -----------------------------------------------------------------------------
|
| 66 |
-
st.title("AI-Powered Text Generation App")
|
| 67 |
-
st.write("Generate text using AI based on your inputs.")
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
topic = st.text_area("Enter the topic or initial text", placeholder="Type your topic here...", height=150)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
"Select the output format",
|
| 76 |
-
options=["Story", "Poem", "Article", "Code"],
|
| 77 |
-
index=0
|
| 78 |
-
)
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
|
| 82 |
-
"Select the tone or style",
|
| 83 |
-
options=["Formal", "Informal", "Humorous", "Technical"],
|
| 84 |
-
index=0
|
| 85 |
-
)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
length = st.slider("Select the approximate word count", min_value=50, max_value=1000, value=200, step=50)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
creative_mode = st.checkbox("Enable creative mode", value=True)
|
| 98 |
-
fact_checking = st.checkbox("Enable fact-checking", value=False)
|
| 99 |
-
|
| 100 |
-
# Button to trigger text generation.
|
| 101 |
if st.button("Generate Text"):
|
| 102 |
-
if
|
| 103 |
-
st.
|
|
|
|
|
|
|
| 104 |
else:
|
| 105 |
-
|
| 106 |
-
# Construct the prompt from the user inputs.
|
| 107 |
-
prompt = generate_prompt(topic, output_format, tone, length, creativity, creative_mode, fact_checking)
|
| 108 |
-
st.write("**Constructed Prompt:**")
|
| 109 |
-
st.code(prompt, language="text")
|
| 110 |
-
|
| 111 |
-
# Call the OpenAI API to generate text responses.
|
| 112 |
-
responses = call_openai_api(prompt, num_responses)
|
| 113 |
-
|
| 114 |
-
if responses:
|
| 115 |
-
st.success("Text generated successfully!")
|
| 116 |
-
# Display each response in a well-formatted manner.
|
| 117 |
-
for idx, response in enumerate(responses, start=1):
|
| 118 |
-
st.markdown(f"### Response {idx}")
|
| 119 |
-
|
| 120 |
-
# Use different formatting based on output format.
|
| 121 |
-
if output_format.lower() == "code":
|
| 122 |
-
st.code(response, language="python")
|
| 123 |
-
else:
|
| 124 |
-
st.write(response)
|
| 125 |
-
|
| 126 |
-
# Feedback section (placeholder for like/dislike buttons and comments)
|
| 127 |
-
col1, col2 = st.columns(2)
|
| 128 |
-
with col1:
|
| 129 |
-
if st.button(f"π Like (Response {idx})"):
|
| 130 |
-
st.info("Thank you for your feedback!")
|
| 131 |
-
with col2:
|
| 132 |
-
if st.button(f"π Dislike (Response {idx})"):
|
| 133 |
-
st.info("Thank you for your feedback!")
|
| 134 |
-
|
| 135 |
-
# Optionally, you can add a text input for comments.
|
| 136 |
-
feedback = st.text_input(f"Leave a comment for Response {idx} (optional)", key=f"feedback_{idx}")
|
| 137 |
-
if feedback:
|
| 138 |
-
st.write("Your comment:", feedback)
|
| 139 |
|
| 140 |
-
#
|
| 141 |
-
# Additional Considerations
|
| 142 |
-
# -----------------------------------------------------------------------------
|
| 143 |
st.markdown("---")
|
| 144 |
-
st.
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import openai
|
| 4 |
|
| 5 |
+
# Load API key securely from environment variables or Streamlit secrets
|
| 6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY", st.secrets.get("OPENAI_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Function to generate AI response
|
| 9 |
+
def call_openai_api(prompt, model, max_tokens, temperature):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
+
response = openai.ChatCompletion.create(
|
| 12 |
+
model=model,
|
| 13 |
+
messages=[{"role": "user", "content": prompt}],
|
| 14 |
+
max_tokens=max_tokens,
|
| 15 |
+
temperature=temperature
|
| 16 |
+
)
|
| 17 |
+
return response["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
except Exception as e:
|
| 19 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Streamlit UI
|
| 22 |
+
st.set_page_config(page_title="AI-Powered Text Generator", layout="centered")
|
|
|
|
| 23 |
|
| 24 |
+
st.title("π AI-Powered Text Generation")
|
| 25 |
+
st.write("Generate text with AI by providing a prompt below!")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# User input fields
|
| 28 |
+
user_input = st.text_area("Enter your prompt here:", height=150)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
col1, col2 = st.columns(2)
|
|
|
|
| 31 |
|
| 32 |
+
with col1:
|
| 33 |
+
model = st.selectbox("Choose AI Model:", ["gpt-3.5-turbo", "gpt-4"])
|
| 34 |
+
max_tokens = st.slider("Max Tokens (Word Length)", min_value=50, max_value=500, value=200, step=50)
|
| 35 |
|
| 36 |
+
with col2:
|
| 37 |
+
temperature = st.slider("Creativity Level (0 = Predictable, 1 = Creative)", min_value=0.0, max_value=1.0, value=0.7, step=0.1)
|
| 38 |
|
| 39 |
+
# Generate button
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if st.button("Generate Text"):
|
| 41 |
+
if user_input.strip():
|
| 42 |
+
st.subheader("Generated Response:")
|
| 43 |
+
result = call_openai_api(user_input, model, max_tokens, temperature)
|
| 44 |
+
st.write(result)
|
| 45 |
else:
|
| 46 |
+
st.warning("β οΈ Please enter a prompt before generating text.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Footer
|
|
|
|
|
|
|
| 49 |
st.markdown("---")
|
| 50 |
+
st.markdown("πΉ *Powered by OpenAI GPT* | πΉ *Developed with Streamlit*")
|