udituen commited on
Commit
ec4695f
·
verified ·
1 Parent(s): c8aae31

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +24 -7
src/streamlit_app.py CHANGED
@@ -4,7 +4,8 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
4
  from langchain.chains import ConversationalRetrievalChain
5
  from langchain_community.llms import HuggingFacePipeline
6
  from langchain.memory import ConversationBufferMemory
7
- from transformers import pipeline
 
8
  import io
9
 
10
  # For PDF processing
@@ -48,16 +49,32 @@ def read_uploaded_file(uploaded_file):
48
  docs = [doc.strip() for doc in docs if doc.strip()]
49
  return docs
50
 
51
- # Load lightweight LLM
52
  @st.cache_resource
53
  def load_llm():
 
 
 
 
 
 
 
 
 
 
 
 
54
  pipe = pipeline(
55
- "text2text-generation",
56
- model="google/flan-t5-large",
57
- max_length=256,
 
58
  temperature=0.7,
59
- top_p=0.95
 
 
60
  )
 
61
  return HuggingFacePipeline(pipeline=pipe)
62
 
63
  # Build retriever from uploaded content
@@ -77,7 +94,7 @@ if 'document_processed' not in st.session_state:
77
  # Streamlit UI
78
  st.title("DocsQA: Chat with Your Document")
79
 
80
- st.markdown("Upload a document and have a conversation about its contents!")
81
 
82
  # Sidebar for document upload
83
  with st.sidebar:
 
4
  from langchain.chains import ConversationalRetrievalChain
5
  from langchain_community.llms import HuggingFacePipeline
6
  from langchain.memory import ConversationBufferMemory
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
8
+ import torch
9
  import io
10
 
11
  # For PDF processing
 
49
  docs = [doc.strip() for doc in docs if doc.strip()]
50
  return docs
51
 
52
+ # Load Qwen LLM
53
  @st.cache_resource
54
  def load_llm():
55
+ model_name = "Qwen/Qwen2.5-1.5B-Instruct" # Using smaller Qwen model for efficiency
56
+
57
+ # Load tokenizer and model
58
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
59
+ model = AutoModelForCausalLM.from_pretrained(
60
+ model_name,
61
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
62
+ device_map="auto" if torch.cuda.is_available() else None,
63
+ trust_remote_code=True
64
+ )
65
+
66
+ # Create pipeline
67
  pipe = pipeline(
68
+ "text-generation",
69
+ model=model,
70
+ tokenizer=tokenizer,
71
+ max_new_tokens=256,
72
  temperature=0.7,
73
+ top_p=0.95,
74
+ do_sample=True,
75
+ return_full_text=False
76
  )
77
+
78
  return HuggingFacePipeline(pipeline=pipe)
79
 
80
  # Build retriever from uploaded content
 
94
  # Streamlit UI
95
  st.title("DocsQA: Chat with Your Document")
96
 
97
+ st.markdown("Upload a document and have a conversation about its contents! (Powered by Qwen)")
98
 
99
  # Sidebar for document upload
100
  with st.sidebar: