kmondal91 commited on
Commit
021244e
·
verified ·
1 Parent(s): 1d61154

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +29 -16
src/streamlit_app.py CHANGED
@@ -1,29 +1,42 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
3
 
4
- # Load the Hugging Face pipeline
5
- chatbot = pipeline("text-generation", model="microsoft/Phi-4-mini-reasoning")
6
 
7
- st.set_page_config(page_title="Chatbot", layout="centered")
 
 
 
 
8
 
9
- st.title("🤖 Simple Chatbot")
10
 
11
- # Initialize chat history
12
  if "chat_history" not in st.session_state:
13
  st.session_state.chat_history = []
14
 
15
- # Input box
16
  user_input = st.text_input("You:", key="input")
17
 
18
  if user_input:
19
- from transformers import Conversation
20
- conversation = Conversation(user_input)
21
- result = chatbot(conversation)
22
-
23
- response = result.generated_responses[-1]
24
  st.session_state.chat_history.append(("You", user_input))
25
- st.session_state.chat_history.append(("Bot", response))
26
 
27
- # Display chat history
28
- for speaker, text in st.session_state.chat_history:
29
- st.write(f"**{speaker}:** {text}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
 
5
+ st.set_page_config(page_title="Qwen Chatbot", layout="centered")
6
+ st.title("🧠 Qwen3-0.6B Chatbot")
7
 
8
+ @st.cache_resource
9
+ def load_model():
10
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
11
+ model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-0.6B", torch_dtype=torch.float32)
12
+ return tokenizer, model
13
 
14
+ tokenizer, model = load_model()
15
 
16
+ # Chat state
17
  if "chat_history" not in st.session_state:
18
  st.session_state.chat_history = []
19
 
 
20
  user_input = st.text_input("You:", key="input")
21
 
22
  if user_input:
23
+ # Add to chat history
 
 
 
 
24
  st.session_state.chat_history.append(("You", user_input))
 
25
 
26
+ # Prepare prompt with full context
27
+ context = ""
28
+ for speaker, msg in st.session_state.chat_history:
29
+ context += f"{speaker}: {msg}\n"
30
+ context += "Bot:"
31
+
32
+ inputs = tokenizer(context, return_tensors="pt")
33
+ outputs = model.generate(**inputs, max_new_tokens=100, do_sample=True, top_p=0.9, temperature=0.7)
34
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
35
+
36
+ # Extract only bot response (after "Bot:")
37
+ bot_msg = response.split("Bot:")[-1].strip()
38
+ st.session_state.chat_history.append(("Bot", bot_msg))
39
+
40
+ # Display conversation
41
+ for speaker, msg in st.session_state.chat_history:
42
+ st.markdown(f"**{speaker}:** {msg}")