Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from langchain_core.prompts import PromptTemplate
|
| 5 |
+
from langchain_community.llms import OpenAI
|
| 6 |
+
from langchain_core.chains import LLMChain
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Fetch API Keys from environment variables
|
| 10 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
|
| 12 |
+
# Verify required environment variables are set
|
| 13 |
+
if not OPENAI_API_KEY:
|
| 14 |
+
raise ValueError("Missing required environment variable: 'OPENAI_API_KEY'")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Title of the App
|
| 18 |
+
st.title("ReAct Agent")
|
| 19 |
+
|
| 20 |
+
# Step 1: User Input Collection
|
| 21 |
+
user_input = st.text_area("Enter the input prompt for the LLM:", placeholder="Describe what you want...")
|
| 22 |
+
num_intermediate_steps = st.number_input("Number of Intermediate Steps to Display:", min_value=1, value=3)
|
| 23 |
+
|
| 24 |
+
# Check if inputs are provided
|
| 25 |
+
if st.button("Run Processing"):
|
| 26 |
+
if not api_key or not user_input:
|
| 27 |
+
st.error("Please provide all required inputs!")
|
| 28 |
+
else:
|
| 29 |
+
st.success("Inputs received! Processing...")
|
| 30 |
+
|
| 31 |
+
# Step 2: LLM Initialization and Template
|
| 32 |
+
st.header("Step 2: LLM Processing - Intermediate Outputs")
|
| 33 |
+
try:
|
| 34 |
+
llm = OpenAI(api_key=api_key, temperature=0.7)
|
| 35 |
+
prompt = PromptTemplate(
|
| 36 |
+
input_variables=["text"],
|
| 37 |
+
template="Process the following text step-by-step: {text}"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
| 41 |
+
intermediate_outputs = []
|
| 42 |
+
|
| 43 |
+
# Simulate multiple intermediate outputs
|
| 44 |
+
for step in range(num_intermediate_steps):
|
| 45 |
+
response = chain.run(user_input + f" Step {step+1}")
|
| 46 |
+
intermediate_outputs.append(response)
|
| 47 |
+
st.write(f"**Intermediate Step {step+1}:**")
|
| 48 |
+
st.info(response)
|
| 49 |
+
|
| 50 |
+
# Step 3: Final Processing
|
| 51 |
+
st.header("Step 3: Visualization")
|
| 52 |
+
# Convert responses to a DataFrame
|
| 53 |
+
df = pd.DataFrame({"Step": [f"Step {i+1}" for i in range(num_intermediate_steps)],
|
| 54 |
+
"Output": intermediate_outputs})
|
| 55 |
+
st.dataframe(df)
|
| 56 |
+
|
| 57 |
+
# Plot the outputs' length as a graph
|
| 58 |
+
st.subheader("Output Length per Step")
|
| 59 |
+
df['Length'] = df['Output'].apply(len)
|
| 60 |
+
fig, ax = plt.subplots()
|
| 61 |
+
ax.bar(df['Step'], df['Length'])
|
| 62 |
+
ax.set_xlabel("Steps")
|
| 63 |
+
ax.set_ylabel("Output Length")
|
| 64 |
+
ax.set_title("Text Length at Each Step")
|
| 65 |
+
st.pyplot(fig)
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.error(f"An error occurred: {str(e)}")
|