Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
!pip install streamlit
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Check if CUDA is available and set the device accordingly
|
| 7 |
+
# device = 0 if torch.cuda.is_available() else -1
|
| 8 |
+
|
| 9 |
+
# Initialize the Phi model pipeline
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_model():
|
| 12 |
+
return pipeline(
|
| 13 |
+
"text-generation",
|
| 14 |
+
model="microsoft/phi-2",
|
| 15 |
+
torch_dtype=torch.bfloat16,
|
| 16 |
+
# device=device,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
phi_model = load_model()
|
| 20 |
+
|
| 21 |
+
# Function to generate response
|
| 22 |
+
def generate_response(prompt, max_length=512):
|
| 23 |
+
response = phi_model(prompt, max_length=max_length, do_sample=True, temperature=0.7)
|
| 24 |
+
return response[0]['generated_text']
|
| 25 |
+
|
| 26 |
+
# Streamlit UI
|
| 27 |
+
st.title("Chain of Thought vs Traditional Reasoning - Phi Model")
|
| 28 |
+
|
| 29 |
+
# User input
|
| 30 |
+
user_question = st.text_input("Enter your question:")
|
| 31 |
+
|
| 32 |
+
if user_question:
|
| 33 |
+
# Generate responses
|
| 34 |
+
with st.spinner("Generating responses..."):
|
| 35 |
+
traditional_prompt = f"Question: {user_question}\nAnswer:"
|
| 36 |
+
cot_prompt = f"Question: {user_question}\nLet's approach this step by step:\n1)"
|
| 37 |
+
|
| 38 |
+
traditional_response = generate_response(traditional_prompt)
|
| 39 |
+
cot_response = generate_response(cot_prompt)
|
| 40 |
+
|
| 41 |
+
# Display results
|
| 42 |
+
st.subheader("Traditional Output")
|
| 43 |
+
st.write(traditional_response)
|
| 44 |
+
|
| 45 |
+
st.subheader("Chain of Thought Reasoning")
|
| 46 |
+
st.write(cot_response)
|
| 47 |
+
|
| 48 |
+
# Add explanatory text
|
| 49 |
+
st.markdown("""
|
| 50 |
+
## About this demo
|
| 51 |
+
This demo showcases the difference between traditional output and chain of thought (CoT) reasoning using the Phi-2 model from Microsoft.
|
| 52 |
+
|
| 53 |
+
- **Traditional Output**: Provides a direct answer to the question.
|
| 54 |
+
- **Chain of Thought Reasoning**: Shows the step-by-step thought process leading to the answer.
|
| 55 |
+
|
| 56 |
+
CoT reasoning often results in more detailed and transparent explanations, which can be helpful for complex problems or when understanding the reasoning process is important.
|
| 57 |
+
""")
|