File size: 1,943 Bytes
5c1ff32
 
 
 
 
 
 
 
 
6a947f8
 
5c1ff32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a947f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c1ff32
 
6a947f8
 
 
 
 
 
5c1ff32
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import streamlit as st
from transformers import pipeline

# model repo ID
model_id = "prd101-wd/phi1_5-bankingqa-merged"

# Load model only once
@st.cache_resource
def load_model():
    #return pipeline("question-answering", model=model_id)
    return pipeline("text-generation", model=model_id, trust_remote_code=True)
                    
# Create a text generation pipeline
pipe = load_model()

# Streamlit UI
st.title("Banking HelpDesk from Finetuned Phi1-5")

st.markdown("Ask a question and the fine-tuned Phi-1.5 model will answer.")

user_input = st.text_area("Your question:", height=100)

if st.button("Ask"):
    if user_input.strip():
        # Format the prompt like Alpaca-style
        prompt = f"### Instruction:\n{user_input}\n\n### Response:\n"
        output = pipe(prompt, max_new_tokens=200, do_sample=True, temperature=0.7)

         # Process output
        if isinstance(output, list) and output:
            answer = output[0]['generated_text']
            # Extract only the response part
            if "### Response:" in answer:
                answer = answer.split("### Response:")[-1].strip()
        else:
            answer = "Unable to generate a response. Please try again."
            

        
        # if isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]:
        #     answer = output[0]["generated_text"]
        # else:
        #     answer = "Unable to generate a response. Please try again."

        # Extract only the model's response (remove prompt part if included in output)
        #answer = output.split("### Response:")[-1].strip()
        # if isinstance(output, str):
        #     answer = output.split("### Response:")[-1].strip()
        # else:
        #     answer = "Unexpected output format. Please try again."
        
        st.markdown("### HelpdeskBot Answer:")
        st.success(answer)
    else:
        st.warning("Please enter a question.")