added docker file and question examples
Browse files- .dockerignore +9 -0
- .gitignore +8 -0
- app/streamlitApp.py +98 -0
- dockerfile +13 -0
- requirements.txt +0 -0
- src/examples.py +14 -0
.dockerignore
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
*.pyd
|
| 5 |
+
*.db
|
| 6 |
+
.env
|
| 7 |
+
.vscode/
|
| 8 |
+
.idea/
|
| 9 |
+
.env
|
.gitignore
CHANGED
|
@@ -1 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
.env
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
*.pyd
|
| 5 |
+
*.db
|
| 6 |
+
.env
|
| 7 |
+
.vscode/
|
| 8 |
+
.idea/
|
| 9 |
.env
|
app/streamlitApp.py
CHANGED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from src.cot import generate_answer
|
| 6 |
+
from src.consistency import self_consistent_answer
|
| 7 |
+
from src.examples import DEFAULT_EXAMPLES
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
st.set_page_config(page_title="Reasonify")
|
| 12 |
+
st.title("Reasoning Playground")
|
| 13 |
+
|
| 14 |
+
st.sidebar.header("Settings")
|
| 15 |
+
mode = st.sidebar.selectbox("Prompting mode", ["Base", "Chain Of Thought"])
|
| 16 |
+
model_id = st.sidebar.selectbox("Model",["llama3-8b-8192"])
|
| 17 |
+
temperature = st.sidebar.slider("Temperature", 0.1, 0.5, 0.7, step=0.1)
|
| 18 |
+
num_paths = st.sidebar.selectbox("Number of Reasoning Paths", [1,3,5])
|
| 19 |
+
|
| 20 |
+
zero_shot = False
|
| 21 |
+
if mode == "Chain Of Thought":
|
| 22 |
+
zero_shot = st.sidebar.selectbox("Zero-Shot prompting", [False, True], index=0)
|
| 23 |
+
|
| 24 |
+
if not zero_shot:
|
| 25 |
+
st.sidebar.markdown("### Few-Shot Examples")
|
| 26 |
+
|
| 27 |
+
examples = []
|
| 28 |
+
for i in range(3):
|
| 29 |
+
with st.sidebar.expander(f"Example {i+1}", expanded=(i == 0)):
|
| 30 |
+
default_q, default_a = DEFAULT_EXAMPLES[i]
|
| 31 |
+
q_edit = st.text_area(f"Question {i+1}", default_q, key=f"q_{i}")
|
| 32 |
+
a_edit = st.text_area(f"Answer {i+1}", default_a, key=f"a_{i}")
|
| 33 |
+
examples.append((q_edit, a_edit))
|
| 34 |
+
else:
|
| 35 |
+
examples = []
|
| 36 |
+
|
| 37 |
+
st.markdown("### Enter your question")
|
| 38 |
+
question = st.text_input("Ask something...")
|
| 39 |
+
|
| 40 |
+
reason, ans = None, None
|
| 41 |
+
|
| 42 |
+
if st.button("Generate"):
|
| 43 |
+
if question.strip():
|
| 44 |
+
st.spinner("Thinking...")
|
| 45 |
+
|
| 46 |
+
mode = "cot" if mode == "Chain Of Thought" else "base"
|
| 47 |
+
|
| 48 |
+
if mode == "base":
|
| 49 |
+
ans = generate_answer(question, model_id, temperature)
|
| 50 |
+
|
| 51 |
+
elif mode == "cot" and num_paths == 1:
|
| 52 |
+
|
| 53 |
+
if not zero_shot:
|
| 54 |
+
reason, ans = generate_answer(question=question,
|
| 55 |
+
model_id=model_id,
|
| 56 |
+
temperature=temperature,
|
| 57 |
+
max_tokens=200,
|
| 58 |
+
mode="cot",
|
| 59 |
+
exampler=DEFAULT_EXAMPLES,
|
| 60 |
+
zero_shot=zero_shot
|
| 61 |
+
)
|
| 62 |
+
else:
|
| 63 |
+
reason, ans = generate_answer(question=question,
|
| 64 |
+
model_id=model_id,
|
| 65 |
+
temperature=temperature,
|
| 66 |
+
max_tokens=200,
|
| 67 |
+
mode="cot",
|
| 68 |
+
zero_shot=zero_shot)
|
| 69 |
+
|
| 70 |
+
else:
|
| 71 |
+
reason, ans = self_consistent_answer(question=question,
|
| 72 |
+
model_id=model_id,
|
| 73 |
+
temperature=temperature,
|
| 74 |
+
max_tokens=200,
|
| 75 |
+
exampler=DEFAULT_EXAMPLES,
|
| 76 |
+
num_samples=num_paths
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
st.markdown("## Output")
|
| 81 |
+
|
| 82 |
+
if mode == "base":
|
| 83 |
+
st.success(f"**Answer:** {ans}")
|
| 84 |
+
elif mode == "cot" and num_paths == 1:
|
| 85 |
+
with st.expander(f"Final Answer: {ans}"):
|
| 86 |
+
st.write(reason)
|
| 87 |
+
else:
|
| 88 |
+
st.markdown("### Self-Consistent Reasoning Paths")
|
| 89 |
+
for i, (r, a) in enumerate(zip(reason, ans)):
|
| 90 |
+
with st.expander(f"Path {i+1}: {a.strip()}"):
|
| 91 |
+
st.write(r.strip())
|
| 92 |
+
|
| 93 |
+
st.success(f"**Final Answer (most common):** {max(set(ans), key=ans.count)}")
|
| 94 |
+
else:
|
| 95 |
+
st.warning("Please enter a question.")
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
EXPOSE 8501
|
| 12 |
+
|
| 13 |
+
CMD ["streamlit", "run", "app/streamlitApp.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
requirements.txt
ADDED
|
Binary file (208 Bytes). View file
|
|
|
src/examples.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
DEFAULT_EXAMPLES = [
|
| 2 |
+
(
|
| 3 |
+
"If A is twice B, and B is 3, what is A?",
|
| 4 |
+
"Let’s think step by step:\nB = 3\nA = 2 * B = 6\nAnswer: 6"
|
| 5 |
+
),
|
| 6 |
+
(
|
| 7 |
+
"If today is Monday, what day will it be in 3 days?",
|
| 8 |
+
"Let’s think step by step:\nMonday + 3 = Thursday\nAnswer: Thursday"
|
| 9 |
+
),
|
| 10 |
+
(
|
| 11 |
+
"If a car travels 60 km in 2 hours, what is the speed?",
|
| 12 |
+
"Let’s think step by step:\nSpeed = Distance / Time = 60 / 2 = 30 km/h\nAnswer: 30"
|
| 13 |
+
),
|
| 14 |
+
]
|