Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,39 @@
|
|
1 |
-
import gradio as gr
|
2 |
import json
|
|
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
# Load
|
6 |
-
|
7 |
-
scheme_data = json.load(file)
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
if query.lower() in scheme["name"].lower() or query.lower() in scheme["description"].lower():
|
13 |
-
return
|
14 |
-
return "Sorry, I couldn't find
|
15 |
|
16 |
-
#
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
def
|
21 |
-
|
22 |
-
if
|
23 |
-
return
|
24 |
-
response =
|
25 |
-
return response
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
demo.launch()
|
|
|
|
|
1 |
import json
|
2 |
+
from flask import Flask, request, jsonify
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Load the model (use a public model)
|
6 |
+
llm = pipeline("text-generation", model="mistralai/Mistral-7B-v0.1")
|
|
|
7 |
|
8 |
+
# Load the schemes data from JSON
|
9 |
+
with open("schemes.json", "r", encoding="utf-8") as f:
|
10 |
+
schemes_data = json.load(f)
|
11 |
+
|
12 |
+
app = Flask(__name__)
|
13 |
+
|
14 |
+
# Function to search for relevant information
|
15 |
+
def find_scheme_info(query):
|
16 |
+
for scheme in schemes_data["schemes"]:
|
17 |
if query.lower() in scheme["name"].lower() or query.lower() in scheme["description"].lower():
|
18 |
+
return scheme["description"]
|
19 |
+
return "Sorry, I couldn't find information on that scheme."
|
20 |
|
21 |
+
# Function to generate a chatbot response
|
22 |
+
def chatbot_response(query):
|
23 |
+
scheme_info = find_scheme_info(query)
|
24 |
+
if scheme_info != "Sorry, I couldn't find information on that scheme.":
|
25 |
+
return scheme_info
|
26 |
+
else:
|
27 |
+
response = llm(query, max_length=200, do_sample=True)
|
28 |
+
return response[0]['generated_text']
|
29 |
|
30 |
+
@app.route("/chat", methods=["POST"])
|
31 |
+
def chat():
|
32 |
+
user_input = request.json.get("query", "")
|
33 |
+
if not user_input:
|
34 |
+
return jsonify({"error": "No query provided"}), 400
|
35 |
+
response = chatbot_response(user_input)
|
36 |
+
return jsonify({"response": response})
|
37 |
|
38 |
+
if __name__ == "__main__":
|
39 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|
|