Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Choose a Flan-T5 model
|
| 5 |
-
# "google/flan-t5-
|
| 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
|
| 11 |
pipe = pipeline(
|
| 12 |
"text2text-generation",
|
| 13 |
model=model_name,
|
|
@@ -16,53 +14,27 @@ pipe = pipeline(
|
|
| 16 |
temperature=0.7
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# A "system prompt"
|
| 20 |
-
|
| 21 |
-
|
| 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 |
-
|
| 26 |
-
"""
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
st.
|
| 33 |
-
st.write("Ask your financial questions, and the assistant will respond with structured, elaborate answers.")
|
| 34 |
|
| 35 |
-
|
| 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)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# 2)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
#
|
| 48 |
-
|
| 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
|
| 62 |
-
st.write("### Conversation")
|
| 63 |
-
for speaker, text in st.session_state.history:
|
| 64 |
-
st.write(f"**{speaker}:** {text}")
|
| 65 |
-
|
| 66 |
-
# Clear Chat button
|
| 67 |
-
if st.button("Clear Chat"):
|
| 68 |
-
st.session_state.history = []
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Choose a Flan-T5 model. "google/flan-t5-large" is a decent balance for CPU.
|
| 5 |
+
# If it's slow, try "google/flan-t5-base". For higher quality, "google/flan-t5-xl" or "google/flan-ul2" (if resources allow).
|
|
|
|
|
|
|
| 6 |
model_name = "google/flan-t5-large"
|
| 7 |
|
| 8 |
+
# Create a text2text-generation pipeline with sampling parameters for some creativity.
|
| 9 |
pipe = pipeline(
|
| 10 |
"text2text-generation",
|
| 11 |
model=model_name,
|
|
|
|
| 14 |
temperature=0.7
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# A "system prompt" that instructs the model to provide structured, elaborate financial guidance.
|
| 18 |
+
system_prompt = """You are a helpful AI assistant specialized in finance.
|
| 19 |
+
You provide thorough, step-by-step, structured guidance using bullet points or headings if relevant.
|
|
|
|
|
|
|
| 20 |
Offer disclaimers that this is not official financial advice, but well-researched educational content.
|
| 21 |
+
Ensure your tone is clear, professional, and detailed."""
|
|
|
|
| 22 |
|
| 23 |
+
# Streamlit UI
|
| 24 |
+
st.title("Flan-T5 Financial Advisor")
|
| 25 |
+
st.write("Ask a financial question and receive a single, detailed response.")
|
| 26 |
|
| 27 |
+
user_input = st.text_area("Enter your financial question here:")
|
|
|
|
| 28 |
|
| 29 |
+
if st.button("Generate"):
|
|
|
|
|
|
|
|
|
|
| 30 |
if user_input.strip():
|
| 31 |
+
# 1) Build a single-turn prompt with system instructions + user question
|
| 32 |
+
prompt = f"{system_prompt}\n\nUser: {user_input}\nAssistant:"
|
| 33 |
+
|
| 34 |
+
# 2) Generate a reply
|
| 35 |
+
output = pipe(prompt, max_length=300)
|
| 36 |
+
answer = output[0]["generated_text"].strip()
|
| 37 |
+
|
| 38 |
+
# 3) Display the model's answer
|
| 39 |
+
st.write("### Assistant's Response")
|
| 40 |
+
st.write(answer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|