Ouiam123 commited on
Commit
e887581
·
verified ·
1 Parent(s): 190622f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -52
app.py CHANGED
@@ -1,57 +1,70 @@
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("tok_read")
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 requests
2
+ import os
3
  from dotenv import load_dotenv
4
+ import time
5
 
6
  load_dotenv() # Load environment variables from .env file
7
 
8
+ # Ensure the Hugging Face API token is set in your environment
9
+ api_token = os.getenv("HF_API_TOKEN")
10
+
11
+ # Check if the token is available
12
+ if api_token is None:
13
+ print("API token is not set. Please set the 'HF_API_TOKEN' environment variable.")
14
+ exit(1)
15
+
16
+ # Set the authorization header with the token
17
+ headers = {
18
+ "Authorization": f"Bearer {api_token}",
19
+ "Content-Type": "application/json"
20
+ }
21
+
22
+ # URL for the Hugging Face model inference
23
+ model_url = "https://api-inference.huggingface.co/models/Ouiam123/Llama-2-7b-chat-finetune-tourism"
24
+
25
+ # Input text you want to send to the model (match the first code's formatting)
26
+ input_text = "What should I do if I get lost in Morocco?"
27
+ formatted_prompt = f"<s>[INST] {input_text} [/INST>"
28
+
29
+ # Request payload
30
+ payload = {
31
+ "inputs": formatted_prompt,
32
+ "parameters": {
33
+ "max_new_tokens": 500,
34
+ "temperature": 0.7,
35
+ "top_p": 0.95,
36
+ "repetition_penalty": 1.15
37
+ }
38
+ }
39
+
40
+ # Function to make the API request with retry on failure
41
+ def get_model_response():
42
+ try:
43
+ response = requests.post(
44
+ model_url,
45
+ headers=headers,
46
+ json=payload,
47
+ timeout=30
48
+ )
49
+
50
+ if response.status_code == 200:
51
+ return response.json() # Return the response if successful
52
+ elif response.status_code == 503: # Retry on service unavailable (503)
53
+ print("Service unavailable, retrying...")
54
+ time.sleep(20) # Wait before retrying
55
+ return get_model_response() # Recursive retry
56
+ else:
57
+ print(f"Error {response.status_code}: {response.text}")
58
+ return None # Return None in case of error
59
+ except requests.exceptions.RequestException as e:
60
+ print(f"Request error: {e}")
61
+ return None # Handle request exceptions
62
+
63
+ # Get the model response
64
+ model_response = get_model_response()
65
+
66
+ # Output the result
67
+ if model_response:
68
+ print("Response:", model_response) # Print the model's response
69
+ else:
70
+ print("Failed to get a valid response from the model.")