Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 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,10 +11,9 @@ 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 |
-
#
|
| 20 |
def get_salesforce_connection():
|
| 21 |
try:
|
| 22 |
sf = Salesforce(
|
|
@@ -31,56 +28,9 @@ def get_salesforce_connection():
|
|
| 31 |
logger.error(f"Error connecting to Salesforce: {e}")
|
| 32 |
return None
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# Define Gradio respond function for chatbot
|
| 38 |
-
def respond(
|
| 39 |
-
message,
|
| 40 |
-
history,
|
| 41 |
-
system_message="You are a helpful assistant.",
|
| 42 |
-
max_tokens=512,
|
| 43 |
-
temperature=0.7,
|
| 44 |
-
top_p=0.95,
|
| 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 |
-
response += token
|
| 67 |
-
yield response
|
| 68 |
-
|
| 69 |
-
# Create Gradio chat interface
|
| 70 |
-
demo = gr.Interface(
|
| 71 |
-
fn=respond,
|
| 72 |
-
inputs=[
|
| 73 |
-
gr.Textbox(label="User Input", placeholder="Type your message here..."),
|
| 74 |
-
gr.State() # State input to maintain conversation history
|
| 75 |
-
],
|
| 76 |
-
outputs=[
|
| 77 |
-
gr.Chatbot(type='messages'), # Corrected to use 'messages' type for the chat
|
| 78 |
-
gr.State() # State output to maintain conversation history
|
| 79 |
-
],
|
| 80 |
-
live=True
|
| 81 |
-
)
|
| 82 |
|
| 83 |
-
# Flask routes
|
| 84 |
@app.route('/')
|
| 85 |
def index():
|
| 86 |
return render_template('index.html')
|
|
@@ -122,10 +72,5 @@ def get_ingredients():
|
|
| 122 |
logger.error(f"Failed to fetch ingredients: {str(e)}")
|
| 123 |
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
|
| 124 |
|
| 125 |
-
# Start Flask and Gradio app
|
| 126 |
if __name__ == '__main__':
|
| 127 |
-
|
| 128 |
-
gradio_thread = demo.launch(share=True, inbrowser=True, server_port=7860, debug=True)
|
| 129 |
-
|
| 130 |
-
# Start Flask app
|
| 131 |
-
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
| 1 |
from flask import Flask, render_template, request, jsonify
|
| 2 |
from simple_salesforce import Salesforce
|
| 3 |
from dotenv import 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 |
+
# Function to get Salesforce connection
|
| 17 |
def get_salesforce_connection():
|
| 18 |
try:
|
| 19 |
sf = Salesforce(
|
|
|
|
| 28 |
logger.error(f"Error connecting to Salesforce: {e}")
|
| 29 |
return None
|
| 30 |
|
| 31 |
+
# Initialize Salesforce connection
|
| 32 |
+
sf = get_salesforce_connection()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
| 34 |
@app.route('/')
|
| 35 |
def index():
|
| 36 |
return render_template('index.html')
|
|
|
|
| 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 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|