mherlie commited on
Commit
07de355
Β·
1 Parent(s): d2f56db

sync with main

Browse files
Files changed (2) hide show
  1. app.py +144 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ import openai
4
+
5
+ # -----------------------------------------------------------------------------
6
+ # Configuration and API Key Setup
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")
12
+
13
+ # -----------------------------------------------------------------------------
14
+ # Helper Functions
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
+ responses = []
44
+ for _ in range(num_responses):
45
+ # You can adjust model name and parameters as needed.
46
+ response = openai.Completion.create(
47
+ engine="text-davinci-003", # or use any model of your choice
48
+ prompt=prompt,
49
+ max_tokens=300, # Adjust based on expected output length
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
+ st.error(f"An error occurred while generating text: {e}")
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
+ # --- Input Fields ---
70
+ # Text area for providing the core topic or initial text.
71
+ topic = st.text_area("Enter the topic or initial text", placeholder="Type your topic here...", height=150)
72
+
73
+ # Dropdown for selecting the desired output format.
74
+ output_format = st.selectbox(
75
+ "Select the output format",
76
+ options=["story", "poem", "article", "code"],
77
+ index=0
78
+ )
79
+
80
+ # Dropdown for selecting the desired tone or style.
81
+ tone = st.selectbox(
82
+ "Select the tone or style",
83
+ options=["formal", "informal", "humorous", "technical"],
84
+ index=0
85
+ )
86
+
87
+ # Slider for controlling the approximate length of the generated text.
88
+ length = st.slider("Select the approximate word count", min_value=50, max_value=1000, value=200, step=50)
89
+
90
+ # Slider for controlling the creativity level.
91
+ creativity = st.slider("Set the creativity level (1: less creative, 10: more creative)", min_value=1, max_value=10, value=7)
92
+
93
+ # Numeric input for specifying the number of responses to generate.
94
+ num_responses = st.number_input("Number of responses to generate", min_value=1, max_value=5, value=1, step=1)
95
+
96
+ # Checkboxes for enabling or disabling specific features.
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 not topic.strip():
103
+ st.warning("Please enter a topic or some initial text before generating content.")
104
+ else:
105
+ with st.spinner("Generating text..."):
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.info("This is a prototype application. In a production environment, ensure to implement proper security, error logging, and user authentication as needed.")
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ openai