Spaces:
Paused
Paused
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +77 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,79 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# --- CONFIGURATION ---
|
| 6 |
+
MODEL_REPO = "bharatgenai/Param2-17B-A2.4B-Thinking"
|
| 7 |
+
# Note: Replace this with the actual GGUF repo once it is confirmed live
|
| 8 |
+
GGUF_REPO = "DarkWolfX/Param2-17B-A2.4B-Thinking-GGUF"
|
| 9 |
+
GGUF_FILE = "param2-thinking-q4_k_m.gguf"
|
| 10 |
+
|
| 11 |
+
# --- UI SETUP ---
|
| 12 |
+
st.set_page_config(page_title="Param2 Multilingual Chat", layout="centered")
|
| 13 |
+
st.title("🇮🇳 Param2-17B Thinking Chatbot")
|
| 14 |
+
|
| 15 |
+
# Language Selection Dropdown
|
| 16 |
+
languages = [
|
| 17 |
+
"English", "Hindi", "Assamese", "Bengali", "Bodo", "Dogri", "Gujarati",
|
| 18 |
+
"Kannada", "Konkani", "Kashmiri", "Maithili", "Malayalam", "Manipuri",
|
| 19 |
+
"Marathi", "Nepali", "Oriya", "Punjabi", "Sanskrit", "Santali",
|
| 20 |
+
"Sindhi", "Tamil", "Telugu", "Urdu"
|
| 21 |
+
]
|
| 22 |
+
selected_lang = st.selectbox("Select Response Language:", languages)
|
| 23 |
+
|
| 24 |
+
# --- MODEL LOADING LOGIC ---
|
| 25 |
+
@st.cache_resource
|
| 26 |
+
def load_model():
|
| 27 |
+
try:
|
| 28 |
+
# 1. Check if GGUF exists and download
|
| 29 |
+
model_path = hf_hub_download(repo_id=GGUF_REPO, filename=GGUF_FILE)
|
| 30 |
+
|
| 31 |
+
# 2. Initialize llama-cpp (Optimized for CPU)
|
| 32 |
+
from llama_cpp import Llama
|
| 33 |
+
llm = Llama(
|
| 34 |
+
model_path=model_path,
|
| 35 |
+
n_ctx=4096,
|
| 36 |
+
n_threads=8, # Optimized for your 8 vCPU Space
|
| 37 |
+
)
|
| 38 |
+
return llm
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error: GGUF model not found or incompatible. {str(e)}"
|
| 41 |
+
|
| 42 |
+
# Attempt to load
|
| 43 |
+
llm = load_model()
|
| 44 |
+
|
| 45 |
+
if isinstance(llm, str):
|
| 46 |
+
st.error(llm)
|
| 47 |
+
st.info("The GGUF version of this model might not be available yet. Please check back later!")
|
| 48 |
+
else:
|
| 49 |
+
# --- CHAT INTERFACE ---
|
| 50 |
+
if "messages" not in st.session_state:
|
| 51 |
+
st.session_state.messages = []
|
| 52 |
+
|
| 53 |
+
for message in st.session_state.messages:
|
| 54 |
+
with st.chat_message(message["role"]):
|
| 55 |
+
st.markdown(message["content"])
|
| 56 |
+
|
| 57 |
+
if prompt := st.chat_input("Ask something..."):
|
| 58 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 59 |
+
with st.chat_message("user"):
|
| 60 |
+
st.markdown(prompt)
|
| 61 |
+
|
| 62 |
+
with st.chat_message("assistant"):
|
| 63 |
+
# System Prompt Injection for Language
|
| 64 |
+
system_instruction = f"You are a helpful assistant. You must respond ONLY in {selected_lang}."
|
| 65 |
+
full_prompt = f"<|system|>\n{system_instruction}\n<|user|>\n{prompt}\n<|assistant|>\n"
|
| 66 |
+
|
| 67 |
+
# Generate response
|
| 68 |
+
response_container = st.empty()
|
| 69 |
+
full_response = ""
|
| 70 |
+
|
| 71 |
+
# Stream the response for a better UI feel
|
| 72 |
+
for chunk in llm(full_prompt, max_tokens=1024, stream=True):
|
| 73 |
+
text = chunk["choices"][0]["text"]
|
| 74 |
+
full_response += text
|
| 75 |
+
response_container.markdown(full_response + "▌")
|
| 76 |
+
|
| 77 |
+
response_container.markdown(full_response)
|
| 78 |
+
|
| 79 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|