Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,7 +1,52 @@
|
|
1 |
-
from flask import Flask
|
|
|
|
|
|
|
|
|
2 |
|
3 |
app = Flask(__name__)
|
4 |
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.route("/")
|
6 |
def hello():
|
7 |
-
return "hello
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from hugchat import hugchat
|
3 |
+
from hugchat.login import Login
|
4 |
+
import os
|
5 |
+
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
+
# Get Hugging Face credentials from environment variables
|
10 |
+
hf_email = os.getenv('HF_EMAIL')
|
11 |
+
hf_pass = os.getenv('HF_PASS')
|
12 |
+
|
13 |
+
|
14 |
@app.route("/")
|
15 |
def hello():
|
16 |
+
return "hello 🤗, Welcome to Sema AI Chat Service."
|
17 |
+
|
18 |
+
# Flask route to handle incoming chat requests
|
19 |
+
@app.route('/chat', methods=['POST'])
|
20 |
+
def chat():
|
21 |
+
# Get JSON data from the POST request
|
22 |
+
data = request.json
|
23 |
+
prompt = data.get('prompt')
|
24 |
+
email = data.get('email')
|
25 |
+
password = data.get('password')
|
26 |
+
|
27 |
+
if not (prompt and email and password):
|
28 |
+
return jsonify({"error": "Missing prompt, email, or password"}), 400
|
29 |
+
|
30 |
+
# Generate the response
|
31 |
+
response = generate_response(prompt, email, password)
|
32 |
+
|
33 |
+
# Return the response as JSON
|
34 |
+
return jsonify({"response": response})
|
35 |
+
|
36 |
+
# Function for generating LLM response
|
37 |
+
def generate_response(prompt_input, email, passwd):
|
38 |
+
# Hugging Face Login
|
39 |
+
sign = Login(email, passwd)
|
40 |
+
cookies = sign.login()
|
41 |
+
# Create ChatBot
|
42 |
+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
43 |
+
|
44 |
+
# Simple dialogue structure
|
45 |
+
string_dialogue = "You are a helpful assistant."
|
46 |
+
string_dialogue += f"\n\nUser: {prompt_input}\n\nAssistant: "
|
47 |
+
|
48 |
+
# Generate and return the response
|
49 |
+
return chatbot.chat(string_dialogue)
|
50 |
+
|
51 |
+
if __name__ == '__main__':
|
52 |
+
app.run(debug=True)
|