RaghadAbdulaziz commited on
Commit
f1a747c
ยท
1 Parent(s): ac382d7

Add application file

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # โœ… Load your model
6
+ model_name = "microsoft/DialoGPT-medium" # Replace with your model if different
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
+
10
+ # โœ… Keyword filter (from your notebook)
11
+ allowed_keywords = [
12
+ "ุนุฌูˆุฉ", "Ajwa", "ุฌุงู„ูƒุณูŠ", "Galaxy", "ู…ุฌุฏูˆู„", "Medjool", "ู…ู†ูŠููŠ",
13
+ "Munifi", "ุณูƒุฑูŠ", "Sukkari", "ุชู…ุฑ", "ุชู…ุฑูŠุฉ", "ู†ุฎูŠู„", "ู†ุฎู„ุฉ", "ู…ุฒุฑุนุฉ", "ุจู„ุญ", "ู…ุงุก", "ุฑูŠ", "ู†ุฎู„", "dates", "date"
14
+ ]
15
+
16
+ # โœ… Chat function with filtering
17
+ def chatbot_fn(message, history=[]):
18
+ # Keyword check
19
+ if not any(keyword.lower() in message.lower() for keyword in allowed_keywords):
20
+ response = "ุฃู†ุง ุฃุฌูŠุจ ูู‚ุท ุนู„ู‰ ุงู„ุฃุณุฆู„ุฉ ุงู„ู…ุชุนู„ู‚ุฉ ุจุงู„ู†ุฎูŠู„ ูˆุงู„ุชู…ูˆุฑุŒ ู‡ู„ ูŠู…ูƒู†ูƒ ุชุญุฏูŠุฏ ุณุคุงู„ูƒ ุฃูƒุซุฑุŸ"
21
+ history.append((message, response))
22
+ return history, history
23
+
24
+ # Encode and generate
25
+ inputs = tokenizer.encode(message + tokenizer.eos_token, return_tensors="pt")
26
+ outputs = model.generate(inputs, max_length=500, pad_token_id=tokenizer.eos_token_id)
27
+ response = tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)
28
+
29
+ history.append((message, response))
30
+ return history, history
31
+
32
+ # โœ… Gradio UI
33
+ demo = gr.ChatInterface(fn=chatbot_fn, title="Lina Chatbot")
34
+
35
+ demo.launch()