Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import
|
| 3 |
import torch
|
| 4 |
|
| 5 |
# Set page configuration
|
|
@@ -7,47 +7,48 @@ st.set_page_config(page_title="Urdu AI Chatbot", page_icon="🤖")
|
|
| 7 |
|
| 8 |
# Title and description
|
| 9 |
st.title("اردو AI چیٹ بوٹ")
|
| 10 |
-
st.write("یہ ایک سادہ چیٹ بوٹ ہے جو اردو زبان میں جواب دیتا ہے۔ اپنا سوال درج
|
| 11 |
|
| 12 |
-
# Load
|
| 13 |
@st.cache_resource
|
| 14 |
def load_model():
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
model =
|
| 18 |
-
tokenizer = MBart50TokenizerFast.from_pretrained(model_name)
|
| 19 |
-
# Set the target language to Urdu
|
| 20 |
-
tokenizer.tgt_lang = "ur_PK"
|
| 21 |
return model, tokenizer
|
| 22 |
except Exception as e:
|
| 23 |
-
st.error(f"
|
| 24 |
return None, None
|
| 25 |
|
| 26 |
model, tokenizer = load_model()
|
| 27 |
if model is None or tokenizer is None:
|
| 28 |
-
st.error("
|
| 29 |
st.stop()
|
| 30 |
|
| 31 |
-
# Function to generate response
|
| 32 |
def generate_response(user_input):
|
| 33 |
try:
|
| 34 |
-
#
|
| 35 |
-
inputs = tokenizer(user_input, return_tensors="pt", padding=True)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
response = tokenizer.decode(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
return response
|
| 45 |
except Exception as e:
|
| 46 |
return f"معذرت، کچھ غلطی ہوئی: {str(e)}"
|
| 47 |
|
| 48 |
# Chat interface
|
| 49 |
if "messages" not in st.session_state:
|
| 50 |
-
st.session_state.messages = [{"role": "assistant", "content": "سلام! میں آپ کی مدد
|
| 51 |
|
| 52 |
# Display chat history
|
| 53 |
for message in st.session_state.messages:
|
|
@@ -56,19 +57,16 @@ for message in st.session_state.messages:
|
|
| 56 |
|
| 57 |
# User input
|
| 58 |
if prompt := st.chat_input("اپنا سوال یہاں لکھیں..."):
|
| 59 |
-
# Add user message to chat history
|
| 60 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 61 |
with st.chat_message("user"):
|
| 62 |
st.write(prompt)
|
| 63 |
|
| 64 |
-
# Generate and display assistant response
|
| 65 |
with st.chat_message("assistant"):
|
| 66 |
-
with st.spinner("
|
| 67 |
response = generate_response(prompt)
|
| 68 |
st.write(response)
|
| 69 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 70 |
|
| 71 |
-
# Sidebar
|
| 72 |
st.sidebar.title("معلومات")
|
| 73 |
-
st.sidebar.write("یہ
|
| 74 |
-
st.sidebar.write("مزید ماڈلز کے لیے [Hugging Face](https://huggingface.co/models) دیکھیں۔")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import DistilBertTokenizer, DistilBertForQuestionAnswering
|
| 3 |
import torch
|
| 4 |
|
| 5 |
# Set page configuration
|
|
|
|
| 7 |
|
| 8 |
# Title and description
|
| 9 |
st.title("اردو AI چیٹ بوٹ")
|
| 10 |
+
st.write("یہ ایک سادہ چیٹ بوٹ ہے جو اردو زبان میں جواب دیتا ہے۔ اپنا سوال درج کریں!")
|
| 11 |
|
| 12 |
+
# Load a lightweight multilingual model
|
| 13 |
@st.cache_resource
|
| 14 |
def load_model():
|
| 15 |
try:
|
| 16 |
+
tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-multilingual-cased")
|
| 17 |
+
model = DistilBertForQuestionAnswering.from_pretrained("distilbert-base-multilingual-cased")
|
|
|
|
|
|
|
|
|
|
| 18 |
return model, tokenizer
|
| 19 |
except Exception as e:
|
| 20 |
+
st.error(f"ماڈل لوڈ کرنے میں خرابی: {str(e)}")
|
| 21 |
return None, None
|
| 22 |
|
| 23 |
model, tokenizer = load_model()
|
| 24 |
if model is None or tokenizer is None:
|
| 25 |
+
st.error("ماڈل یا ٹوکنائزر لوڈ نہیں ہوا۔ براہ کرم لاگز چیک کریں۔")
|
| 26 |
st.stop()
|
| 27 |
|
| 28 |
+
# Function to generate a simple response
|
| 29 |
def generate_response(user_input):
|
| 30 |
try:
|
| 31 |
+
# Encode the input
|
| 32 |
+
inputs = tokenizer(user_input, return_tensors="pt", truncation=True, padding=True)
|
| 33 |
+
outputs = model(**inputs)
|
| 34 |
+
# Since this is a QA model, we'll simulate a response
|
| 35 |
+
start_scores = outputs.start_logits
|
| 36 |
+
end_scores = outputs.end_logits
|
| 37 |
+
start_idx = torch.argmax(start_scores)
|
| 38 |
+
end_idx = torch.argmax(end_scores) + 1
|
| 39 |
+
answer_tokens = inputs["input_ids"][0][start_idx:end_idx]
|
| 40 |
+
response = tokenizer.decode(answer_tokens, skip_special_tokens=True)
|
| 41 |
+
|
| 42 |
+
# If the response is empty or too short, provide a fallback
|
| 43 |
+
if not response or len(response) < 3:
|
| 44 |
+
return "میں سمجھ گیا، لیکن براہ کرم مزید وضاحت کریں۔"
|
| 45 |
return response
|
| 46 |
except Exception as e:
|
| 47 |
return f"معذرت، کچھ غلطی ہوئی: {str(e)}"
|
| 48 |
|
| 49 |
# Chat interface
|
| 50 |
if "messages" not in st.session_state:
|
| 51 |
+
st.session_state.messages = [{"role": "assistant", "content": "سلام! میں آپ کی مدد کے لیے حاضر ہوں۔"}]
|
| 52 |
|
| 53 |
# Display chat history
|
| 54 |
for message in st.session_state.messages:
|
|
|
|
| 57 |
|
| 58 |
# User input
|
| 59 |
if prompt := st.chat_input("اپنا سوال یہاں لکھیں..."):
|
|
|
|
| 60 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 61 |
with st.chat_message("user"):
|
| 62 |
st.write(prompt)
|
| 63 |
|
|
|
|
| 64 |
with st.chat_message("assistant"):
|
| 65 |
+
with st.spinner("جواب تیار کر رہا ہوں..."):
|
| 66 |
response = generate_response(prompt)
|
| 67 |
st.write(response)
|
| 68 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 69 |
|
| 70 |
+
# Sidebar
|
| 71 |
st.sidebar.title("معلومات")
|
| 72 |
+
st.sidebar.write("یہ `distilbert-base-multilingual-cased` ماڈل استعمال کرتا ہے۔")
|
|
|