Spaces:
Sleeping
Sleeping
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}"} | |
def home(): | |
return render_template('index.html') | |
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) | |