Mohamed2002 commited on
Commit
010583e
1 Parent(s): 1b09332

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -42
app.py CHANGED
@@ -1,42 +1,37 @@
1
- from flask import Flask, request, jsonify
2
- from transformers import AutoModelForSeq2SeqLM, pipeline
3
-
4
- # Load the previously trained and saved model
5
- model = AutoModelForSeq2SeqLM.from_pretrained('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
-
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
- @app.route('/chat', methods=['POST'])
20
- def chat():
21
- data = request.json
22
- user_input = data.get('message')
23
-
24
- if not user_input:
25
- return jsonify({'error': 'No input message provided'}), 400
26
-
27
- try:
28
- # Translate user input from Arabic to English
29
- translated_input = translate(user_input, translator_ar_en)
30
-
31
- # Generate prediction
32
- prediction = custom_model(translated_input)[0]['generated_text']
33
-
34
- # Translate response from English to Arabic
35
- translated_response = translate(prediction, translator_en_ar)
36
-
37
- return jsonify({'response': translated_response})
38
- except Exception as e:
39
- return jsonify({'error': str(e)}), 500
40
-
41
- if __name__ == '__main__':
42
- app.run(host='0.0.0.0', port=5000)
 
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()
37
+