Spaces:
Sleeping
Sleeping
File size: 2,528 Bytes
0399328 834001a 0399328 733916f 60c0d29 834001a bb41b4b 834001a 0399328 834001a bb41b4b b90dee0 bb41b4b 01a885d ad15d33 b90dee0 01a885d b90dee0 0399328 b90dee0 7e80aef b90dee0 7e80aef 0399328 0643817 ad15d33 6dd3dbf 688b851 ad15d33 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
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 = request.json.get("message")
if not user_message:
return jsonify({"error": "Empty message received"})
try:
formatted_prompt = f"User: {user_message}\nBot:"
payload = {
"inputs": formatted_prompt,
"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": ["User:", "You:", "user:"] # β 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']}"})
# raw_output = data[0]['generated_text'] if isinstance(data, list) else data.get('generated_text', "No response")
# reply = raw_output.split("Bot:")[-1].strip()
# return jsonify({"reply": reply})
raw_output = data[0]['generated_text'] if isinstance(data, list) else data.get('generated_text', "No response")
# Clean up output
if "Bot:" in raw_output:
reply = raw_output.split("Bot:")[-1].strip()
else:
reply = raw_output.strip()
for trailing in ["User", "You", "User:", "You:"]:
if reply.endswith(trailing):
reply = reply.rsplit(trailing, 1)[0].strip()
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)
|