Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,72 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
def main():
|
4 |
st.title("Problem Statement and Factor Guess")
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
st.write("
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
if __name__ == "__main__":
|
23 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
3 |
+
|
4 |
+
def generate_more_factors(problem_statement, user_factors):
|
5 |
+
# 1. Prepare prompt text
|
6 |
+
factors_text = "\n".join([f"- {factor}" for factor in user_factors if factor.strip() != ""])
|
7 |
+
prompt = (
|
8 |
+
f"You are an expert problem solver. Given the following problem statement:\n"
|
9 |
+
f"{problem_statement}\n\n"
|
10 |
+
f"And the following user-provided factors:\n"
|
11 |
+
f"{factors_text}\n\n"
|
12 |
+
f"Please suggest additional factors that would complete a MECE (Mutually Exclusive, Collectively Exhaustive) "
|
13 |
+
f"set of factors responsible for solving the problem. Provide your suggestions as a bullet list."
|
14 |
+
)
|
15 |
+
|
16 |
+
# 2. Call Hugging Face inference API
|
17 |
+
# Replace 'gpt2' with another model if needed.
|
18 |
+
API_URL = "https://api-inference.huggingface.co/models/gpt2"
|
19 |
+
token = st.secrets.get("HF_API_TOKEN", "")
|
20 |
+
headers = {"Authorization": f"Bearer {token}"} if token else {}
|
21 |
+
|
22 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
|
23 |
+
if response.status_code == 200:
|
24 |
+
result = response.json()
|
25 |
+
if isinstance(result, list) and result and "generated_text" in result[0]:
|
26 |
+
# Remove the prompt from the generated output if it is repeated.
|
27 |
+
generated = result[0]["generated_text"]
|
28 |
+
suggestions = generated[len(prompt):].strip()
|
29 |
+
return suggestions
|
30 |
+
else:
|
31 |
+
return "Unexpected response format."
|
32 |
+
else:
|
33 |
+
return f"Error: {response.status_code} - {response.text}"
|
34 |
|
35 |
def main():
|
36 |
st.title("Problem Statement and Factor Guess")
|
37 |
+
st.write("Provide your problem statement and suggest factors (one per row). You can add additional rows as needed.")
|
38 |
+
|
39 |
+
# 1. Problem Statement
|
40 |
+
problem_statement = st.text_input("1. Enter your problem statement:")
|
41 |
|
42 |
+
st.write("2. Enter your factors (each factor in a separate textbox):")
|
43 |
|
44 |
+
# 2. Dynamic factor inputs stored in session_state
|
45 |
+
if "factor_rows" not in st.session_state:
|
46 |
+
st.session_state.factor_rows = [""]
|
47 |
|
48 |
+
factor_inputs = []
|
49 |
+
for i in range(len(st.session_state.factor_rows)):
|
50 |
+
key = f"factor_{i}"
|
51 |
+
# Display a text input for each factor row.
|
52 |
+
value = st.text_input(f"Factor {i+1}", value=st.session_state.factor_rows[i], key=key)
|
53 |
+
factor_inputs.append(value)
|
54 |
+
st.session_state.factor_rows[i] = value
|
55 |
|
56 |
+
# 3. Button to add more rows
|
57 |
+
if st.button("Add Factor Row"):
|
58 |
+
st.session_state.factor_rows.append("")
|
59 |
+
st.experimental_rerun()
|
60 |
|
61 |
+
# 4. Button to generate more factors using the LLM
|
62 |
+
if st.button("Generate More Factors"):
|
63 |
+
if not problem_statement.strip():
|
64 |
+
st.error("Please enter a problem statement before generating additional factors.")
|
65 |
+
else:
|
66 |
+
with st.spinner("Generating more factors..."):
|
67 |
+
suggestions = generate_more_factors(problem_statement, factor_inputs)
|
68 |
+
st.markdown("### Generated Factors")
|
69 |
+
st.write(suggestions)
|
70 |
|
71 |
if __name__ == "__main__":
|
72 |
main()
|