Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,124 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 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 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from langdetect import detect
|
| 4 |
+
from translator import translate
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
# Config
|
| 10 |
+
LOCAL_MODEL_DIR = "guvi_gpt2_finetuned" # local path if you fine-tuned locally
|
| 11 |
+
USE_LOCAL = os.path.isdir(LOCAL_MODEL_DIR)
|
| 12 |
+
HF_MODEL_ID = "your-username/guvi_gpt2_finetuned" # if you use HF InferenceClient instead
|
| 13 |
|
| 14 |
+
# Load tokenizer + model (local)
|
| 15 |
+
@st.cache_resource
|
| 16 |
+
def load_local_model(model_dir):
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(model_dir)
|
| 19 |
+
model.eval()
|
| 20 |
+
if torch.cuda.is_available():
|
| 21 |
+
model.to("cuda")
|
| 22 |
+
return tokenizer, model
|
| 23 |
|
| 24 |
+
if USE_LOCAL:
|
| 25 |
+
tokenizer, model = load_local_model(LOCAL_MODEL_DIR)
|
| 26 |
+
else:
|
| 27 |
+
tokenizer = None
|
| 28 |
+
model = None
|
| 29 |
+
# if you want use HF Inference API, you'll use streaming_inference.py instead
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
st.set_page_config(page_title="GUVI Multilingual Chatbot", layout="wide")
|
| 32 |
+
st.title("GUVI Multilingual Chatbot")
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Sidebar controls
|
| 35 |
+
with st.sidebar:
|
| 36 |
+
st.header("Settings")
|
| 37 |
+
max_new_tokens = st.slider("Max new tokens", 50, 512, 200)
|
| 38 |
+
temperature = st.slider("Temperature", 0.1, 1.5, 0.7)
|
| 39 |
+
top_p = st.slider("Top-p", 0.1, 1.0, 0.95)
|
| 40 |
+
use_auto_detect = st.checkbox("Auto-detect input language (recommended)", value=True)
|
| 41 |
+
source_lang_override = st.text_input("Force source lang code (e.g. tam_Taml) — leave empty for auto-detect")
|
| 42 |
|
| 43 |
+
# Chat UI
|
| 44 |
+
if "history" not in st.session_state:
|
| 45 |
+
st.session_state.history = []
|
| 46 |
|
| 47 |
+
def detect_lang_code(text):
|
| 48 |
+
# detect returns 'en', 'hi', etc. We need NLLB code mapping
|
| 49 |
+
try:
|
| 50 |
+
short = detect(text)
|
| 51 |
+
except:
|
| 52 |
+
short = "en"
|
| 53 |
+
# basic mapping - expand as required
|
| 54 |
+
mapping = {
|
| 55 |
+
"en": "eng_Latn",
|
| 56 |
+
"hi": "hin_Deva",
|
| 57 |
+
"ta": "tam_Taml",
|
| 58 |
+
"te": "tel_Telu",
|
| 59 |
+
"kn": "kan_Knda",
|
| 60 |
+
"mr": "mar_Deva",
|
| 61 |
+
"bn": "ben_Beng",
|
| 62 |
+
# add more mappings
|
| 63 |
+
}
|
| 64 |
+
return mapping.get(short, "eng_Latn")
|
| 65 |
|
| 66 |
+
def generate_reply(prompt_en):
|
| 67 |
+
"""
|
| 68 |
+
Generate reply in English using local model or HF inference API.
|
| 69 |
+
Returns English text.
|
| 70 |
+
"""
|
| 71 |
+
if USE_LOCAL and model is not None:
|
| 72 |
+
input_text = "Q: " + prompt_en + "\nA:"
|
| 73 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt")
|
| 74 |
+
if torch.cuda.is_available():
|
| 75 |
+
inputs = inputs.to("cuda")
|
| 76 |
+
model.to("cuda")
|
| 77 |
+
outputs = model.generate(inputs, max_new_tokens=max_new_tokens, temperature=temperature, top_p=top_p, pad_token_id=tokenizer.eos_token_id)
|
| 78 |
+
reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 79 |
+
# extract the answer portion after "A:"
|
| 80 |
+
if "\nA:" in reply:
|
| 81 |
+
reply = reply.split("\nA:")[-1].strip()
|
| 82 |
+
return reply
|
| 83 |
+
else:
|
| 84 |
+
# If not local, fallback to instructing user to use HF Inference API
|
| 85 |
+
return "Model not loaded locally. Push model to Hugging Face Hub and switch to InferenceClient or run locally."
|
| 86 |
|
| 87 |
+
# Input area
|
| 88 |
+
user_input = st.text_area("Enter your message", height=120)
|
| 89 |
+
if st.button("Send"):
|
| 90 |
+
if not user_input.strip():
|
| 91 |
+
st.warning("Please enter a message.")
|
| 92 |
+
else:
|
| 93 |
+
# language detection / translation
|
| 94 |
+
if source_lang_override.strip():
|
| 95 |
+
src_code = source_lang_override.strip()
|
| 96 |
+
elif use_auto_detect:
|
| 97 |
+
src_code = detect_lang_code(user_input)
|
| 98 |
+
else:
|
| 99 |
+
src_code = "eng_Latn"
|
| 100 |
|
| 101 |
+
needs_translation = src_code != "eng_Latn"
|
| 102 |
+
if needs_translation:
|
| 103 |
+
# translate to English
|
| 104 |
+
st.session_state.history.append(("User (original): " + user_input, ""))
|
| 105 |
+
prompt_en = translate(user_input, src_lang_code=src_code, tgt_lang_code="eng_Latn")
|
| 106 |
+
else:
|
| 107 |
+
prompt_en = user_input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
# get english answer
|
| 110 |
+
reply_en = generate_reply(prompt_en)
|
| 111 |
|
| 112 |
+
# translate back if needed
|
| 113 |
+
if needs_translation:
|
| 114 |
+
reply_local = translate(reply_en, src_lang_code="eng_Latn", tgt_lang_code=src_code)
|
| 115 |
+
else:
|
| 116 |
+
reply_local = reply_en
|
| 117 |
+
|
| 118 |
+
st.session_state.history.append((user_input, reply_local))
|
| 119 |
+
|
| 120 |
+
# Show chat history
|
| 121 |
+
for user_msg, bot_msg in reversed(st.session_state.history):
|
| 122 |
+
st.markdown(f"**User:** {user_msg}")
|
| 123 |
+
st.markdown(f"**Bot:** {bot_msg}")
|
| 124 |
+
st.write("---")
|