Spaces:
Runtime error
Runtime error
Echo-ai
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
from gradio_client import Client
|
3 |
+
import threading
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# Initialize the Gradio client
|
8 |
+
client = Client("m-ric/open_Deep-Research")
|
9 |
+
|
10 |
+
def interact_with_agent(messages):
|
11 |
+
try:
|
12 |
+
response = client.predict(
|
13 |
+
messages=messages,
|
14 |
+
api_name="/interact_with_agent"
|
15 |
+
)
|
16 |
+
return response
|
17 |
+
except Exception as e:
|
18 |
+
return {"error": str(e)}
|
19 |
+
|
20 |
+
@app.route('/')
|
21 |
+
def index():
|
22 |
+
return render_template('index.html')
|
23 |
+
|
24 |
+
@app.route('/search', methods=['POST'])
|
25 |
+
def search():
|
26 |
+
data = request.get_json()
|
27 |
+
query = data.get('query', '').strip()
|
28 |
+
if not query:
|
29 |
+
return jsonify({"error": "No query provided."}), 400
|
30 |
+
|
31 |
+
# Initialize the conversation with the user's query
|
32 |
+
conversation = [
|
33 |
+
{"role": "user", "content": query}
|
34 |
+
]
|
35 |
+
|
36 |
+
def background_task():
|
37 |
+
try:
|
38 |
+
# First, get the initial response from the API
|
39 |
+
initial_response = client.predict(
|
40 |
+
text_input=query,
|
41 |
+
api_name="/log_user_message"
|
42 |
+
)
|
43 |
+
|
44 |
+
# Append the assistant's initial response to the conversation
|
45 |
+
conversation.append({"role": "assistant", "content": initial_response})
|
46 |
+
|
47 |
+
# Now, interact with the agent using the conversation
|
48 |
+
final_response = interact_with_agent(conversation)
|
49 |
+
|
50 |
+
# Extract the final answer from the response
|
51 |
+
final_answer = None
|
52 |
+
for message in final_response:
|
53 |
+
if message.get('role') == 'assistant' and 'Final answer' in message.get('content', ''):
|
54 |
+
final_answer = message.get('content', '').split('Final answer:')[-1].strip()
|
55 |
+
break
|
56 |
+
|
57 |
+
# Send the result back to the client
|
58 |
+
socketio.emit('result', {'query': query, 'result': final_answer})
|
59 |
+
except Exception as e:
|
60 |
+
socketio.emit('result', {'query': query, 'result': f"An error occurred: {str(e)}"})
|
61 |
+
|
62 |
+
# Start the background thread
|
63 |
+
thread = threading.Thread(target=background_task)
|
64 |
+
thread.start()
|
65 |
+
|
66 |
+
return jsonify({"status": "Processing..."}), 200
|
67 |
+
|
68 |
+
if __name__ == '__main__':
|
69 |
+
from flask_socketio import SocketIO
|
70 |
+
|
71 |
+
# Initialize SocketIO for real-time updates
|
72 |
+
socketio = SocketIO(app, cors_allowed_origins="*")
|
73 |
+
|
74 |
+
@socketio.on('connect')
|
75 |
+
def handle_connect():
|
76 |
+
print('Client connected')
|
77 |
+
|
78 |
+
@socketio.on('disconnect')
|
79 |
+
def handle_disconnect():
|
80 |
+
print('Client disconnected')
|
81 |
+
|
82 |
+
# Run the Flask app with SocketIO
|
83 |
+
socketio.run(app, host='0.0.0.0', port=7860)
|