monesh2212 commited on
Commit
8edc97c
·
verified ·
1 Parent(s): ba7ddcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -50
app.py CHANGED
@@ -1,13 +1,11 @@
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,53 +14,27 @@ pipe = pipeline(
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
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)