Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from flask import Flask, render_template, request, jsonify
|
| 2 |
from simple_salesforce import Salesforce
|
| 3 |
from dotenv import load_dotenv
|
|
@@ -11,9 +13,10 @@ load_dotenv()
|
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
|
|
|
| 14 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
| 15 |
|
| 16 |
-
#
|
| 17 |
def get_salesforce_connection():
|
| 18 |
try:
|
| 19 |
sf = Salesforce(
|
|
@@ -28,9 +31,60 @@ def get_salesforce_connection():
|
|
| 28 |
logger.error(f"Error connecting to Salesforce: {e}")
|
| 29 |
return None
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
@app.route('/')
|
| 35 |
def index():
|
| 36 |
return render_template('index.html')
|
|
@@ -72,5 +126,10 @@ def get_ingredients():
|
|
| 72 |
logger.error(f"Failed to fetch ingredients: {str(e)}")
|
| 73 |
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
|
| 74 |
|
|
|
|
| 75 |
if __name__ == '__main__':
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
from flask import Flask, render_template, request, jsonify
|
| 4 |
from simple_salesforce import Salesforce
|
| 5 |
from dotenv import load_dotenv
|
|
|
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
# Initialize Flask app
|
| 17 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
| 18 |
|
| 19 |
+
# Initialize Salesforce connection
|
| 20 |
def get_salesforce_connection():
|
| 21 |
try:
|
| 22 |
sf = Salesforce(
|
|
|
|
| 31 |
logger.error(f"Error connecting to Salesforce: {e}")
|
| 32 |
return None
|
| 33 |
|
| 34 |
+
# Connect to HuggingFace model
|
| 35 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 36 |
+
|
| 37 |
+
# Define Gradio respond function for chatbot
|
| 38 |
+
def respond(
|
| 39 |
+
message,
|
| 40 |
+
history: list[tuple[str, str]],
|
| 41 |
+
system_message,
|
| 42 |
+
max_tokens,
|
| 43 |
+
temperature,
|
| 44 |
+
top_p,
|
| 45 |
+
):
|
| 46 |
+
messages = [{"role": "system", "content": system_message}]
|
| 47 |
+
|
| 48 |
+
for val in history:
|
| 49 |
+
if val[0]:
|
| 50 |
+
messages.append({"role": "user", "content": val[0]})
|
| 51 |
+
if val[1]:
|
| 52 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 53 |
+
|
| 54 |
+
messages.append({"role": "user", "content": message})
|
| 55 |
|
| 56 |
+
response = ""
|
| 57 |
+
|
| 58 |
+
for message in client.chat_completion(
|
| 59 |
+
messages,
|
| 60 |
+
max_tokens=max_tokens,
|
| 61 |
+
stream=True,
|
| 62 |
+
temperature=temperature,
|
| 63 |
+
top_p=top_p,
|
| 64 |
+
):
|
| 65 |
+
token = message.choices[0].delta.content
|
| 66 |
+
|
| 67 |
+
response += token
|
| 68 |
+
yield response
|
| 69 |
+
|
| 70 |
+
# Create Gradio chat interface
|
| 71 |
+
demo = gr.ChatInterface(
|
| 72 |
+
respond,
|
| 73 |
+
additional_inputs=[
|
| 74 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 75 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 76 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 77 |
+
gr.Slider(
|
| 78 |
+
minimum=0.1,
|
| 79 |
+
maximum=1.0,
|
| 80 |
+
value=0.95,
|
| 81 |
+
step=0.05,
|
| 82 |
+
label="Top-p (nucleus sampling)",
|
| 83 |
+
),
|
| 84 |
+
],
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Flask routes
|
| 88 |
@app.route('/')
|
| 89 |
def index():
|
| 90 |
return render_template('index.html')
|
|
|
|
| 126 |
logger.error(f"Failed to fetch ingredients: {str(e)}")
|
| 127 |
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
|
| 128 |
|
| 129 |
+
# Start Flask and Gradio app
|
| 130 |
if __name__ == '__main__':
|
| 131 |
+
# Launch Gradio chat interface
|
| 132 |
+
gradio_thread = demo.launch(share=True, inbrowser=True, server_port=7860, debug=True)
|
| 133 |
+
|
| 134 |
+
# Start Flask app
|
| 135 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|