Spaces:
Runtime error
Runtime error
Mohamed2002
commited on
Commit
•
40b7807
1
Parent(s):
0f3ca65
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,36 @@
|
|
1 |
-
from flask import Flask, request, jsonify
|
2 |
from transformers import AutoModelForSeq2SeqLM, pipeline
|
3 |
|
4 |
# Load the previously trained and saved model
|
5 |
-
|
|
|
6 |
|
7 |
# Load the translation models
|
8 |
translator_ar_en = pipeline('translation_ar_to_en', model='Helsinki-NLP/opus-mt-ar-en')
|
9 |
translator_en_ar = pipeline('translation_en_to_ar', model='Helsinki-NLP/opus-mt-en-ar')
|
10 |
-
|
11 |
# Create the custom model pipeline
|
12 |
-
custom_model = pipeline('text2text-generation', model='product_model')
|
13 |
-
|
14 |
-
app = Flask(__name__)
|
15 |
|
16 |
def translate(text, translator):
|
17 |
-
return translator(text)[0]['translation_text']
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
if not user_input:
|
26 |
-
return jsonify({'error': 'No input message provided'}), 400
|
27 |
-
|
28 |
-
try:
|
29 |
-
# Translate user input from Arabic to English
|
30 |
-
translated_input = translate(user_input, translator_ar_en)
|
31 |
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
1 |
from transformers import AutoModelForSeq2SeqLM, pipeline
|
2 |
|
3 |
# Load the previously trained and saved model
|
4 |
+
model_path = r'C:\Users\muham\OneDrive\Desktop\certificates\product_model'
|
5 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(r'C:\Users\muham\OneDrive\Desktop\certificates\product_model')
|
6 |
|
7 |
# Load the translation models
|
8 |
translator_ar_en = pipeline('translation_ar_to_en', model='Helsinki-NLP/opus-mt-ar-en')
|
9 |
translator_en_ar = pipeline('translation_en_to_ar', model='Helsinki-NLP/opus-mt-en-ar')
|
|
|
10 |
# Create the custom model pipeline
|
11 |
+
custom_model = pipeline('text2text-generation', model=r'C:\Users\muham\OneDrive\Desktop\certificates\product_model')
|
|
|
|
|
12 |
|
13 |
def translate(text, translator):
|
14 |
+
return translator(text)[0]['translation_text']
|
15 |
+
def chat_with_bot():
|
16 |
+
print("(:اهلا بحضرتك في تطبيق السمسار الالكتروني")
|
17 |
+
while True:
|
18 |
+
# Get user input
|
19 |
+
user_input = input("العميل: ")
|
20 |
+
if user_input.lower() == 'quit':
|
21 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Translate user input from Arabic to English
|
24 |
+
try:
|
25 |
+
translated_input = translate(user_input, translator_ar_en)
|
26 |
+
|
27 |
+
# Generate prediction
|
28 |
+
prediction = custom_model(translated_input)[0]['generated_text']
|
29 |
+
# Translate response from English to Arabic
|
30 |
+
translated_response = translate(prediction, translator_en_ar)
|
31 |
+
print("السمسار:", translated_response)
|
32 |
+
except Exception as e:
|
33 |
+
print(f"An error occurred: {str(e)}")
|
34 |
+
|
35 |
+
# Start chatting with the bot
|
36 |
+
chat_with_bot()
|