Spaces:
Running
Running
Utkarsh Verma
commited on
Commit
·
0399328
1
Parent(s):
9b87b51
Adding Docker File
Browse files
app.py
CHANGED
@@ -1,11 +1,14 @@
|
|
1 |
-
import
|
2 |
import os
|
3 |
from flask import Flask, request, jsonify, render_template
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
-
# Set your
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
@app.route('/')
|
11 |
def home():
|
@@ -17,19 +20,19 @@ def chat():
|
|
17 |
|
18 |
if not user_message:
|
19 |
return jsonify({"error": "Empty message received"})
|
20 |
-
|
21 |
try:
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
28 |
return jsonify({"reply": reply})
|
29 |
|
30 |
-
except
|
31 |
-
|
32 |
-
return jsonify({"reply": f"Error in response: {str(e)}"}) # Show error in UI
|
33 |
|
34 |
if __name__ == '__main__':
|
35 |
app.run(host='0.0.0.0', port=7860)
|
|
|
1 |
+
import requests
|
2 |
import os
|
3 |
from flask import Flask, request, jsonify, render_template
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
# Set your Hugging Face API key
|
8 |
+
HF_API_KEY = os.getenv("HF_API_KEY") # Store in environment variable
|
9 |
+
|
10 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct"
|
11 |
+
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
12 |
|
13 |
@app.route('/')
|
14 |
def home():
|
|
|
20 |
|
21 |
if not user_message:
|
22 |
return jsonify({"error": "Empty message received"})
|
23 |
+
|
24 |
try:
|
25 |
+
response = requests.post(API_URL, headers=HEADERS, json={"inputs": user_message})
|
26 |
+
data = response.json()
|
27 |
+
|
28 |
+
if "error" in data:
|
29 |
+
return jsonify({"reply": f"Error: {data['error']}"})
|
30 |
+
|
31 |
+
reply = data[0]['generated_text'] if isinstance(data, list) else data.get('generated_text', "No response")
|
32 |
return jsonify({"reply": reply})
|
33 |
|
34 |
+
except Exception as e:
|
35 |
+
return jsonify({"reply": f"Error: {str(e)}"})
|
|
|
36 |
|
37 |
if __name__ == '__main__':
|
38 |
app.run(host='0.0.0.0', port=7860)
|