Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pip install gradio transformers
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
4 |
+
|
5 |
+
class ArabicAssistant:
|
6 |
+
def __init__(self):
|
7 |
+
self.model_name = "EleutherAI/gpt-neo-2.7B"
|
8 |
+
self.tokenizer = GPT2Tokenizer.from_pretrained(self.model_name)
|
9 |
+
self.model = GPT2LMHeadModel.from_pretrained(self.model_name)
|
10 |
+
|
11 |
+
def generate_response(self, input_text):
|
12 |
+
input_ids = self.tokenizer.encode(input_text, return_tensors="pt")
|
13 |
+
output = self.model.generate(input_ids, max_length=100, num_beams=5, no_repeat_ngram_size=2, top_k=50, top_p=0.95, temperature=0.7)
|
14 |
+
response = self.tokenizer.decode(output[0], skip_special_tokens=True)
|
15 |
+
return response
|
16 |
+
|
17 |
+
assistant = ArabicAssistant()
|
18 |
+
|
19 |
+
def chatbot_interaction(user_input):
|
20 |
+
user_input = user_input.strip()
|
21 |
+
|
22 |
+
# Check if the user input is in Arabic-Egyptian dialect
|
23 |
+
if not any(char in "اأإآبتثجحخدذرزسشصضطظعغفقكلمنهوي" for char in user_input):
|
24 |
+
return "عذرًا، أنا أتحدث فقط باللهجة المصرية. يرجى استخدام اللهجة المصرية في التفاعل معي."
|
25 |
+
|
26 |
+
# Generate response using the chatbot model
|
27 |
+
response = assistant.generate_response(user_input)
|
28 |
+
|
29 |
+
# Add an open-ended question to encourage further conversation
|
30 |
+
response += " أهلاً! كيف يمكنني مساعدتك اليوم؟"
|
31 |
+
|
32 |
+
return response
|
33 |
+
|
34 |
+
iface = gr.Interface(fn=chatbot_interaction, inputs="text", outputs="text", live=True, theme="huggingface")
|
35 |
+
iface.launch()
|