Spaces:
Runtime error
Runtime error
File size: 2,552 Bytes
40bd592 9166cfa 222b769 d0afe8c d865fe0 ce0a61c 0149dab f7170a2 40bd592 0149dab f7170a2 47f121f ce0a61c 47f121f ce0a61c c891d1b d865fe0 c891d1b d865fe0 d0afe8c 9313ec8 f44c554 d0afe8c 3aadc9c d0afe8c f7170a2 ce0a61c 3aadc9c ce0a61c 0dc3a8e ce0a61c 3aadc9c f7170a2 ce0a61c 3aadc9c d0afe8c f7170a2 d0afe8c 0149dab faf11db 0149dab 06c70c9 47f121f 06c70c9 40bd592 c891d1b 0149dab 9313ec8 f7170a2 7fe21e8 06c70c9 ce0a61c 06c70c9 d1b59c5 11a2fb0 |
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 |
import logging
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
from gradio_client import Client
import os
import traceback
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
logging.basicConfig(level=logging.DEBUG)
# Initialize the client (this loads the API)
CLIENT_URL = os.getenv("CLIENT_URL")
MODEL = os.getenv("MODEL")
if not CLIENT_URL:
raise ValueError("CLIENT_URL environment variable must be set")
if not MODEL:
raise ValueError("MODEL environment variable must be set")
app.logger.info(f"Initializing client with URL: {CLIENT_URL}")
client = Client('Nymbo/Groq-Playground-Master')
# System message
system_message = os.getenv("SYS")
context = [{"role": "system", "content": system_message}]
def chat_with_ai(message):
app.logger.debug(f"Received message: {message}")
context.append({"role": "user", "content": message})
# Prepare the full prompt
full_prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in context])
app.logger.debug(f"Making API call with prompt: {full_prompt[:100]}...") # Log first 100 chars of prompt
try:
result = client.predict(
message=full_prompt,
request=MODEL,
param_3=0.5,
param_4=8192,
param_5=0.5,
param_6=0,
api_name="/chat"
)
app.logger.debug(f"Received result: {result[:100]}...") # Log first 100 chars of result
except Exception as e:
app.logger.error(f"Error in API call: {str(e)}")
app.logger.error(traceback.format_exc())
raise
# Add AI response to context
context.append({"role": "assistant", "content": result})
return result
@app.route('/')
def index():
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
data = request.json
user_input = data.get('user_input')
if not user_input:
return jsonify({"error": "No user input provided"}), 400
app.logger.debug(f"Received request: {data}")
try:
response = chat_with_ai(user_input)
app.logger.debug(f"API response content: {response[:100]}...") # Log first 100 chars of response
return jsonify({"response": response})
except Exception as e:
app.logger.error(f"Unexpected error: {str(e)}")
app.logger.error(traceback.format_exc())
return jsonify({"error": f"An unexpected error occurred: {str(e)}"}), 500
if __name__ == "__main__":
app.run(host='0.0.0.0', port=7860) |