Spaces:
Sleeping
Sleeping
Utkarsh Verma
commited on
Commit
Β·
01a885d
1
Parent(s):
74e7f7c
Code Change
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ app = Flask(__name__)
|
|
7 |
# Set your Hugging Face API key
|
8 |
HF_API_KEY = os.getenv("HF_API_KEY") # Store in environment variable
|
9 |
|
10 |
-
API_URL = "https://api-inference.huggingface.co/models/
|
11 |
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
12 |
|
13 |
@app.route('/')
|
@@ -15,23 +15,22 @@ def home():
|
|
15 |
return render_template('index.html')
|
16 |
|
17 |
@app.route('/chat', methods=['POST'])
|
18 |
-
def chat():
|
19 |
user_message = request.json.get("message")
|
20 |
|
21 |
if not user_message:
|
22 |
return jsonify({"error": "Empty message received"})
|
23 |
|
24 |
try:
|
25 |
-
formatted_prompt = f"User: {user_message}\nBot:"
|
26 |
payload = {
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
}
|
34 |
}
|
|
|
35 |
response = requests.post(API_URL, headers=headers, json=payload)
|
36 |
data = response.json()
|
37 |
# print(response.status_code) # Debugging: Print the HTTP status
|
@@ -48,10 +47,6 @@ def chat():
|
|
48 |
return jsonify({"reply": f"Error: {data['error']}"})
|
49 |
|
50 |
reply = data[0]['generated_text'] if isinstance(data, list) else data.get('generated_text', "No response")
|
51 |
-
|
52 |
-
#raw_output = data[0]['generated_text'] if isinstance(data, list) else data.get('generated_text', "No response")
|
53 |
-
# Extract only the part after "Bot:"
|
54 |
-
#reply = raw_output.split("Bot:")[-1].strip()
|
55 |
return jsonify({"reply": reply})
|
56 |
|
57 |
except Exception as e:
|
|
|
7 |
# Set your Hugging Face API key
|
8 |
HF_API_KEY = os.getenv("HF_API_KEY") # Store in environment variable
|
9 |
|
10 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
11 |
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
12 |
|
13 |
@app.route('/')
|
|
|
15 |
return render_template('index.html')
|
16 |
|
17 |
@app.route('/chat', methods=['POST'])
|
18 |
+
def chat(user_input):
|
19 |
user_message = request.json.get("message")
|
20 |
|
21 |
if not user_message:
|
22 |
return jsonify({"error": "Empty message received"})
|
23 |
|
24 |
try:
|
|
|
25 |
payload = {
|
26 |
+
"inputs": f"[INST] {user_input} [/INST]",
|
27 |
+
"parameters": {
|
28 |
+
"temperature": 0.5, # π₯ Controls randomness (lower = more deterministic)
|
29 |
+
"top_p": 0.9, # π― Focus on high-probability words
|
30 |
+
"max_new_tokens": 50, # β³ Limits response length
|
31 |
+
"stop_sequences": ["\nUser:", "[INST]"] # β Stops response at natural points
|
|
|
32 |
}
|
33 |
+
}
|
34 |
response = requests.post(API_URL, headers=headers, json=payload)
|
35 |
data = response.json()
|
36 |
# print(response.status_code) # Debugging: Print the HTTP status
|
|
|
47 |
return jsonify({"reply": f"Error: {data['error']}"})
|
48 |
|
49 |
reply = data[0]['generated_text'] if isinstance(data, list) else data.get('generated_text', "No response")
|
|
|
|
|
|
|
|
|
50 |
return jsonify({"reply": reply})
|
51 |
|
52 |
except Exception as e:
|