Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 (using a sample model; replace as needed)
|
17 |
+
API_URL = "https://api-inference.huggingface.co/models/gpt2"
|
18 |
+
token = st.secrets.get("HF_API_TOKEN", "")
|
19 |
+
headers = {"Authorization": f"Bearer {token}"} if token else {}
|
20 |
+
|
21 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
|
22 |
+
if response.status_code == 200:
|
23 |
+
result = response.json()
|
24 |
+
if isinstance(result, list) and result and "generated_text" in result[0]:
|
25 |
+
generated = result[0]["generated_text"]
|
26 |
+
# Remove the prompt from the generated output
|
27 |
+
suggestions = generated[len(prompt):].strip()
|
28 |
+
return suggestions
|
29 |
+
else:
|
30 |
+
return "Unexpected response format."
|
31 |
+
else:
|
32 |
+
return f"Error: {response.status_code} - {response.text}"
|
33 |
+
|
34 |
+
def main():
|
35 |
+
st.title("Problem Statement and Factor Guess")
|
36 |
+
st.write("Enter your problem statement and factors. The UI is organized into four horizontal levels:")
|
37 |
+
|
38 |
+
# Initialize session state variables
|
39 |
+
if "factor_rows" not in st.session_state:
|
40 |
+
st.session_state.factor_rows = [""]
|
41 |
+
if "llm_suggestions" not in st.session_state:
|
42 |
+
st.session_state.llm_suggestions = None
|
43 |
+
|
44 |
+
# Create four columns for the four levels
|
45 |
+
col1, col2, col3, col4 = st.columns(4)
|
46 |
+
|
47 |
+
# Level 1: Problem Statement (Left-most column)
|
48 |
+
with col1:
|
49 |
+
st.header("Level 1: Problem Statement")
|
50 |
+
problem_statement = st.text_input("Enter your problem statement:")
|
51 |
+
|
52 |
+
# Level 2: User-Provided Factors
|
53 |
+
with col2:
|
54 |
+
st.header("Level 2: Your Factors")
|
55 |
+
factor_inputs = []
|
56 |
+
for i in range(len(st.session_state.factor_rows)):
|
57 |
+
key = f"factor_{i}"
|
58 |
+
value = st.text_input(f"Factor {i+1}", value=st.session_state.factor_rows[i], key=key)
|
59 |
+
factor_inputs.append(value)
|
60 |
+
st.session_state.factor_rows[i] = value
|
61 |
+
|
62 |
+
if st.button("Add Factor Row", key="add_row"):
|
63 |
+
st.session_state.factor_rows.append("")
|
64 |
+
try:
|
65 |
+
st.experimental_rerun()
|
66 |
+
except AttributeError:
|
67 |
+
st.write("Row added. Please refresh the page to see the new row.")
|
68 |
+
|
69 |
+
# Level 3: Generate More Factors Button
|
70 |
+
with col3:
|
71 |
+
st.header("Level 3: Generate More Factors")
|
72 |
+
if st.button("Generate More Factors", key="generate_factors"):
|
73 |
+
if not problem_statement.strip():
|
74 |
+
st.error("Please enter a problem statement before generating factors.")
|
75 |
+
else:
|
76 |
+
with st.spinner("Generating more factors..."):
|
77 |
+
suggestions = generate_more_factors(problem_statement, factor_inputs)
|
78 |
+
st.session_state.llm_suggestions = suggestions
|
79 |
+
|
80 |
+
# Level 4: LLM Suggestions Display
|
81 |
+
with col4:
|
82 |
+
st.header("Level 4: LLM Suggestions")
|
83 |
+
if st.session_state.llm_suggestions:
|
84 |
+
st.write(st.session_state.llm_suggestions)
|
85 |
+
else:
|
86 |
+
st.write("LLM suggestions will appear here after you click 'Generate More Factors'.")
|
87 |
+
|
88 |
+
if __name__ == "__main__":
|
89 |
+
main()
|