Ouiam123 commited on
Commit
814d73e
·
verified ·
1 Parent(s): 5332280

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -57
app.py CHANGED
@@ -1,57 +1,57 @@
1
- import os
2
- from flask import Flask, render_template, request, jsonify
3
- import requests
4
- import time
5
- from dotenv import load_dotenv
6
-
7
- load_dotenv() # Load environment variables from .env file
8
-
9
- app = Flask(__name__)
10
-
11
- class APIInferenceChatbot:
12
- def __init__(self, api_token):
13
- self.api_url = "https://api-inference.huggingface.co/models/Ouiam123/Llama-2-7b-chat-finetune-tourism"
14
- self.headers = {
15
- "Authorization": f"Bearer {api_token}",
16
- "Content-Type": "application/json"
17
- }
18
-
19
- def generate_response(self, input_text):
20
- formatted_prompt = f"<s>[INST] {input_text} [/INST>"
21
- payload = {
22
- "inputs": formatted_prompt,
23
- "parameters": {
24
- "max_new_tokens": 500,
25
- "temperature": 0.7,
26
- "top_p": 0.95,
27
- "repetition_penalty": 1.15
28
- }
29
- }
30
- response = requests.post(self.api_url, headers=self.headers, json=payload, timeout=30)
31
- if response.status_code == 503:
32
- time.sleep(20)
33
- response = requests.post(self.api_url, headers=self.headers, json=payload)
34
- response.raise_for_status()
35
- result = response.json()
36
- return result[0].get('generated_text', '').strip() if isinstance(result, list) and result else str(result)
37
-
38
- # Get the API token from the environment variable
39
- api_token = os.getenv("HF_API_TOKEN")
40
- if not api_token:
41
- raise ValueError("Hugging Face API token not found in environment variables!")
42
-
43
- # Initialize the chatbot with the API token from the environment
44
- chatbot = APIInferenceChatbot(api_token)
45
-
46
- @app.route('/')
47
- def home():
48
- return render_template('index.html') # Serve the HTML file
49
-
50
- @app.route('/chat', methods=['POST'])
51
- def chat():
52
- message = request.json.get('message', '')
53
- response = chatbot.generate_response(message)
54
- return jsonify({'response': response}) # Return the response as JSON
55
-
56
- if __name__ == '__main__':
57
- app.run(host='0.0.0.0', port=7860, debug=True)
 
1
+ import os
2
+ from flask import Flask, render_template, request, jsonify
3
+ import requests
4
+ import time
5
+ from dotenv import load_dotenv
6
+
7
+ load_dotenv() # Load environment variables from .env file
8
+
9
+ app = Flask(__name__)
10
+
11
+ class APIInferenceChatbot:
12
+ def __init__(self, api_token):
13
+ self.api_url = "https://api-inference.huggingface.co/models/Ouiam123/Llama-2-7b-chat-finetune-tourism"
14
+ self.headers = {
15
+ "Authorization": f"Bearer {api_token}",
16
+ "Content-Type": "application/json"
17
+ }
18
+
19
+ def generate_response(self, input_text):
20
+ formatted_prompt = f"<s>[INST] {input_text} [/INST>"
21
+ payload = {
22
+ "inputs": formatted_prompt,
23
+ "parameters": {
24
+ "max_new_tokens": 500,
25
+ "temperature": 0.7,
26
+ "top_p": 0.95,
27
+ "repetition_penalty": 1.15
28
+ }
29
+ }
30
+ response = requests.post(self.api_url, headers=self.headers, json=payload, timeout=30)
31
+ if response.status_code == 503:
32
+ time.sleep(20)
33
+ response = requests.post(self.api_url, headers=self.headers, json=payload)
34
+ response.raise_for_status()
35
+ result = response.json()
36
+ return result[0].get('generated_text', '').strip() if isinstance(result, list) and result else str(result)
37
+
38
+ # Get the API token from the environment variable
39
+ api_token = os.getenv("tok1")
40
+ if not api_token:
41
+ raise ValueError("Hugging Face API token not found in environment variables!")
42
+
43
+ # Initialize the chatbot with the API token from the environment
44
+ chatbot = APIInferenceChatbot(api_token)
45
+
46
+ @app.route('/')
47
+ def home():
48
+ return render_template('index.html') # Serve the HTML file
49
+
50
+ @app.route('/chat', methods=['POST'])
51
+ def chat():
52
+ message = request.json.get('message', '')
53
+ response = chatbot.generate_response(message)
54
+ return jsonify({'response': response}) # Return the response as JSON
55
+
56
+ if __name__ == '__main__':
57
+ app.run(host='0.0.0.0', port=7860, debug=True)