Spaces:
Runtime error
Runtime error
| import os | |
| from flask import Flask, request, jsonify, render_template | |
| import requests | |
| from flask_cors import CORS | |
| app = Flask(__name__) | |
| CORS(app) | |
| # Load the Hugging Face API key from an environment variable | |
| API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-hf" | |
| HEADERS = {"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"} | |
| def query_huggingface(payload): | |
| try: | |
| response = requests.post(API_URL, headers=HEADERS, json=payload) | |
| response.raise_for_status() | |
| return response.json() | |
| except requests.exceptions.RequestException as e: | |
| print("Error querying Hugging Face API:", e) | |
| return {"error": "Connection to Hugging Face API failed"} | |
| def home(): | |
| return render_template("index.html") | |
| def chat(): | |
| user_input = request.json.get("input") | |
| if not user_input: | |
| return jsonify({"response": "Please provide an input"}), 400 | |
| payload = {"inputs": user_input} | |
| response_data = query_huggingface(payload) | |
| if "error" in response_data: | |
| return jsonify({"response": "Sorry, there was an issue processing your request."}), 500 | |
| bot_response = response_data.get("generated_text", "Sorry, I couldn't understand.") | |
| return jsonify({"response": bot_response}) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860, debug=True) | |