pradeep4321 commited on
Commit
f3c6eb8
Β·
verified Β·
1 Parent(s): e74f40b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +10 -14
src/streamlit_app.py CHANGED
@@ -10,11 +10,11 @@ st.set_page_config(page_title="πŸ€– AI Assistant", layout="wide")
10
  st.title("πŸ€– Simple AI Assistant")
11
 
12
  # ==============================
13
- # LOAD MODEL
14
  # ==============================
15
  @st.cache_resource
16
  def load_model():
17
- model_name = "deepseek-ai/deepseek-coder-1.3b-instruct"
18
 
19
  tokenizer = AutoTokenizer.from_pretrained(model_name)
20
 
@@ -38,11 +38,10 @@ if "messages" not in st.session_state:
38
  st.session_state.messages = []
39
 
40
  # ==============================
41
- # CLEAN OUTPUT
42
  # ==============================
43
  def clean_text(text):
44
  text = re.sub(r"[^\x00-\x7F]+", "", text)
45
- text = text.replace("```", "")
46
  return text.strip()
47
 
48
  # ==============================
@@ -50,17 +49,16 @@ def clean_text(text):
50
  # ==============================
51
  def generate_response(user_input):
52
 
53
- prompt = f"""User: {user_input}
54
- Assistant:"""
55
 
56
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
57
 
58
  with torch.no_grad():
59
  outputs = model.generate(
60
  **inputs,
61
- max_new_tokens=120,
62
  do_sample=True,
63
- temperature=0.5,
64
  top_p=0.9,
65
  repetition_penalty=1.1,
66
  pad_token_id=tokenizer.eos_token_id
@@ -68,9 +66,9 @@ Assistant:"""
68
 
69
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
70
 
71
- # Extract only assistant reply
72
- if "Assistant:" in result:
73
- result = result.split("Assistant:")[-1]
74
 
75
  return clean_text(result)
76
 
@@ -82,18 +80,16 @@ for msg in st.session_state.messages:
82
  st.markdown(msg["content"])
83
 
84
  # ==============================
85
- # INPUT BOX
86
  # ==============================
87
  user_input = st.chat_input("Type your message...")
88
 
89
  if user_input:
90
- # User message
91
  st.session_state.messages.append({"role": "user", "content": user_input})
92
 
93
  with st.chat_message("user"):
94
  st.markdown(user_input)
95
 
96
- # AI response
97
  with st.spinner("πŸ€– Thinking..."):
98
  response = generate_response(user_input)
99
 
 
10
  st.title("πŸ€– Simple AI Assistant")
11
 
12
  # ==============================
13
+ # LOAD MODEL (CHAT MODEL βœ…)
14
  # ==============================
15
  @st.cache_resource
16
  def load_model():
17
+ model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" # πŸ”₯ BEST FOR HF FREE
18
 
19
  tokenizer = AutoTokenizer.from_pretrained(model_name)
20
 
 
38
  st.session_state.messages = []
39
 
40
  # ==============================
41
+ # CLEAN TEXT
42
  # ==============================
43
  def clean_text(text):
44
  text = re.sub(r"[^\x00-\x7F]+", "", text)
 
45
  return text.strip()
46
 
47
  # ==============================
 
49
  # ==============================
50
  def generate_response(user_input):
51
 
52
+ prompt = f"<|user|>\n{user_input}\n<|assistant|>\n"
 
53
 
54
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
55
 
56
  with torch.no_grad():
57
  outputs = model.generate(
58
  **inputs,
59
+ max_new_tokens=150,
60
  do_sample=True,
61
+ temperature=0.7,
62
  top_p=0.9,
63
  repetition_penalty=1.1,
64
  pad_token_id=tokenizer.eos_token_id
 
66
 
67
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
68
 
69
+ # Extract assistant part
70
+ if "<|assistant|>" in result:
71
+ result = result.split("<|assistant|>")[-1]
72
 
73
  return clean_text(result)
74
 
 
80
  st.markdown(msg["content"])
81
 
82
  # ==============================
83
+ # INPUT
84
  # ==============================
85
  user_input = st.chat_input("Type your message...")
86
 
87
  if user_input:
 
88
  st.session_state.messages.append({"role": "user", "content": user_input})
89
 
90
  with st.chat_message("user"):
91
  st.markdown(user_input)
92
 
 
93
  with st.spinner("πŸ€– Thinking..."):
94
  response = generate_response(user_input)
95