Spaces:
Sleeping
Sleeping
File size: 1,983 Bytes
0399328 834001a 0399328 733916f 60c0d29 834001a 733916f 834001a 0399328 834001a b90dee0 733916f 01a885d b90dee0 01a885d b90dee0 0399328 b90dee0 7e80aef b90dee0 7e80aef 0399328 45bb5d9 834001a e5b71df 0399328 834001a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import requests
import os
from flask import Flask, request, jsonify, render_template
app = Flask(__name__)
# Set your Hugging Face API key
HF_API_KEY = os.getenv("HF_API_KEY") # Store in environment variable
API_URL = "https://api-inference.huggingface.co/models/tiiuae/falcon-7b-instruct"
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
@app.route('/')
def home():
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat(user_message):
user_message = request.json.get("message")
if not user_message:
return jsonify({"error": "Empty message received"})
try:
payload = {
"inputs": f"User: {user_message}\nBot:",
#"inputs": f"[INST] {user_input} [/INST]",
"parameters": {
"temperature": 0.5, # π₯ Controls randomness (lower = more deterministic)
"top_p": 0.9, # π― Focus on high-probability words
"max_new_tokens": 50, # β³ Limits response length
"stop_sequences": ["\nUser:", "[INST]"] # β Stops response at natural points
}
}
response = requests.post(API_URL, headers=headers, json=payload)
data = response.json()
# print(response.status_code) # Debugging: Print the HTTP status
# print(response.json()) # Debugging: Print the API response
# if response.status_code == 200:
# return response.json()[0]['generated_text']
# else:
# return f"Error: {response.status_code} - {response.json()}"
if "error" in data:
return jsonify({"reply": f"Error: {data['error']}"})
reply = data[0]['generated_text'] if isinstance(data, list) else data.get('generated_text', "No response")
return jsonify({"reply": reply})
except Exception as e:
return jsonify({"reply": f"Error: {str(e)}"})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)
|