File size: 4,330 Bytes
0399328
834001a
 
 
 
 
0399328
 
 
733916f
60c0d29
834001a
1ff88ce
834001a
 
 
 
 
bb41b4b
834001a
1ff88ce
834001a
1ff88ce
0399328
834001a
2388d5a
b90dee0
2388d5a
1ff88ce
 
 
 
afc89dd
1ff88ce
b90dee0
1ff88ce
b90dee0
0399328
7e80aef
1ff88ce
 
 
 
 
 
 
 
 
 
95ae9c6
 
1ff88ce
 
326eced
1ff88ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e80aef
 
0399328
1ff88ce
 
0643817
1ff88ce
 
6dd3dbf
688b851
1ff88ce
ad15d33
1ff88ce
 
 
 
 
 
ad15d33
1ff88ce
 
 
ad15d33
1ff88ce
e5b71df
1ff88ce
 
834001a
1ff88ce
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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}"}


@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({"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)