Spaces:
Running
Running
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({"reply": "\u26a0\ufe0f No message provided."}) | |
try: | |
formatted_prompt = f"User: {user_message}\nBot:" | |
payload = { | |
"inputs": formatted_prompt, | |
"parameters": { | |
"temperature": 0.5, | |
"top_p": 0.9, | |
"max_new_tokens": 100, | |
"stop_sequences": ["\nUser:", "\nYou:", "\nBot:"] | |
} | |
} | |
response = requests.post(API_URL, headers=headers, json=payload) | |
data = response.json() | |
if "error" in data: | |
return jsonify({"reply": f"\u274c Error: {data['error']}"}) | |
# Extract response safely | |
if isinstance(data, list) and 'generated_text' in data[0]: | |
bot_reply = data[0]['generated_text'] | |
elif 'generated_text' in data: | |
bot_reply = data['generated_text'] | |
else: | |
bot_reply = "\ud83e\udde0 Sorry, I didn't understand that." | |
if user_message in bot_reply: | |
bot_reply = bot_reply.replace(user_message, "").strip() | |
# Clean extra prompt echoes like "User:" | |
cleaned_reply = bot_reply.replace("User:", "").replace("You:", "").replace("Bot:", "").strip() | |
cleaned_reply = cleaned_reply.rstrip("User").rstrip("Bot").strip() | |
return jsonify({"reply": cleaned_reply}) | |
except Exception as e: | |
return jsonify({"reply": f"\ud83d\udea8 Server Error: {str(e)}"}) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=7860, debug=True) | |
# @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) | |