Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Choose a Flan-T5 model (
|
| 5 |
-
# "google/flan-t5-
|
| 6 |
-
# "google/flan-t5-
|
|
|
|
| 7 |
model_name = "google/flan-t5-large"
|
| 8 |
|
| 9 |
-
# Create a text2text-generation pipeline with some sampling parameters
|
| 10 |
-
# If you prefer more deterministic outputs, remove or lower do_sample/temperature
|
| 11 |
pipe = pipeline(
|
| 12 |
"text2text-generation",
|
| 13 |
model=model_name,
|
|
@@ -16,37 +16,46 @@ pipe = pipeline(
|
|
| 16 |
temperature=0.7
|
| 17 |
)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Initialize conversation history in session_state
|
| 20 |
if "history" not in st.session_state:
|
| 21 |
st.session_state.history = []
|
| 22 |
|
| 23 |
-
st.title("Flan-T5
|
| 24 |
-
st.write("
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
user_input = st.text_input("Type your message here:")
|
| 28 |
|
| 29 |
-
# Send button
|
| 30 |
if st.button("Send"):
|
| 31 |
if user_input.strip():
|
| 32 |
-
# Add user message to the history
|
| 33 |
st.session_state.history.append(("User", user_input))
|
| 34 |
|
| 35 |
-
# Build
|
| 36 |
-
conversation_text = ""
|
| 37 |
for speaker, text in st.session_state.history:
|
| 38 |
conversation_text += f"{speaker}: {text}\n"
|
|
|
|
| 39 |
conversation_text += "Assistant:"
|
| 40 |
|
| 41 |
-
# Generate a reply
|
| 42 |
output = pipe(
|
| 43 |
conversation_text,
|
| 44 |
-
max_length=
|
| 45 |
)
|
| 46 |
-
# Flan-T5
|
| 47 |
assistant_reply = output[0]["generated_text"].strip()
|
| 48 |
|
| 49 |
-
# Add
|
| 50 |
st.session_state.history.append(("Assistant", assistant_reply))
|
| 51 |
|
| 52 |
# Display the conversation
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Choose a Flan-T5 model (public and instruction-tuned).
|
| 5 |
+
# "google/flan-t5-large" is a good balance of performance and quality on CPU.
|
| 6 |
+
# If it's slow, try "google/flan-t5-base". If you want better responses (and can handle bigger CPU load),
|
| 7 |
+
# try "google/flan-t5-xl" or "google/flan-ul2".
|
| 8 |
model_name = "google/flan-t5-large"
|
| 9 |
|
| 10 |
+
# Create a text2text-generation pipeline with some sampling parameters.
|
|
|
|
| 11 |
pipe = pipeline(
|
| 12 |
"text2text-generation",
|
| 13 |
model=model_name,
|
|
|
|
| 16 |
temperature=0.7
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# A "system prompt" or "prebuilt prompt" that sets the context for financial guidance
|
| 20 |
+
# and encourages structured, elaborate answers.
|
| 21 |
+
system_prompt = """
|
| 22 |
+
You are a helpful AI assistant specialized in finance.
|
| 23 |
+
You provide thorough, step-by-step, structured guidance, using bullet points or headings if relevant.
|
| 24 |
+
Offer disclaimers that this is not official financial advice, but well-researched educational content.
|
| 25 |
+
Whenever you respond, ensure the tone is clear, professional, and detailed.
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
# Initialize conversation history in session_state
|
| 29 |
if "history" not in st.session_state:
|
| 30 |
st.session_state.history = []
|
| 31 |
|
| 32 |
+
st.title("Financial Guidance Chatbot (Flan-T5)")
|
| 33 |
+
st.write("Ask your financial questions, and the assistant will respond with structured, elaborate answers.")
|
| 34 |
|
| 35 |
+
# User input
|
| 36 |
+
user_input = st.text_input("Type your question or message here:")
|
| 37 |
|
|
|
|
| 38 |
if st.button("Send"):
|
| 39 |
if user_input.strip():
|
| 40 |
+
# 1) Add user's message to the conversation history
|
| 41 |
st.session_state.history.append(("User", user_input))
|
| 42 |
|
| 43 |
+
# 2) Build the full prompt (system instructions + entire conversation)
|
| 44 |
+
conversation_text = system_prompt.strip() + "\n\n"
|
| 45 |
for speaker, text in st.session_state.history:
|
| 46 |
conversation_text += f"{speaker}: {text}\n"
|
| 47 |
+
# The final line signals the assistant to respond
|
| 48 |
conversation_text += "Assistant:"
|
| 49 |
|
| 50 |
+
# 3) Generate a reply
|
| 51 |
output = pipe(
|
| 52 |
conversation_text,
|
| 53 |
+
max_length=300 # Increase for more elaborate answers
|
| 54 |
)
|
| 55 |
+
# Flan-T5 returns a list of dicts with "generated_text"
|
| 56 |
assistant_reply = output[0]["generated_text"].strip()
|
| 57 |
|
| 58 |
+
# 4) Add assistant's reply to the history
|
| 59 |
st.session_state.history.append(("Assistant", assistant_reply))
|
| 60 |
|
| 61 |
# Display the conversation
|