Mohamed2002 commited on
Commit
40b7807
1 Parent(s): 0f3ca65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -32
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
- 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
-
20
- @app.route('/chat', methods=['POST'])
21
- def chat():
22
- data = request.json
23
- user_input = data.get('message')
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
- # Generate prediction
33
- prediction = custom_model(translated_input)[0]['generated_text']
34
-
35
- # Translate response from English to Arabic
36
- translated_response = translate(prediction, translator_en_ar)
37
-
38
- return jsonify({'response': translated_response})
39
- except Exception as e:
40
- return jsonify({'error': str(e)}), 500
41
-
42
- if __name__ == '__main__':
43
- 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()