Spaces:
Runtime error
Runtime error
| import os | |
| import io | |
| import json | |
| import tempfile | |
| import pandas as pd | |
| from flask import Flask, request, jsonify, send_file | |
| from flask_cors import CORS | |
| from dotenv import load_dotenv | |
| from groq_llms import LLMHandler | |
| # Load environment variables | |
| load_dotenv() | |
| app = Flask(__name__) | |
| CORS(app) # Enable CORS for all routes | |
| # Initialize LLM Handler | |
| llm_handler = LLMHandler() | |
| def process_csv(file, user_prompt): | |
| """ | |
| Process CSV file and generate responses using LLMHandler | |
| Args: | |
| file (werkzeug.datastructures.FileStorage): Uploaded CSV file | |
| user_prompt (str): Prompt for invitation generation | |
| Returns: | |
| pandas.DataFrame: DataFrame with generated invitations | |
| """ | |
| try: | |
| # Read CSV directly from file storage | |
| df = pd.read_csv(file) | |
| responses = [] | |
| for _, row in df.iterrows(): | |
| try: | |
| response = llm_handler.generate_response(user_prompt, row.to_dict()) | |
| responses.append(response) | |
| except Exception as e: | |
| responses.append(f"Error: {e}") | |
| df["Generated Text"] = responses | |
| return df | |
| except Exception as e: | |
| raise ValueError(f"Error processing CSV: {str(e)}") | |
| def generate_questions(): | |
| """ | |
| Generate questions based on initial context | |
| Request Payload: | |
| { | |
| "context": "Initial context for invitation" | |
| } | |
| Returns: | |
| JSON array of questions | |
| """ | |
| data = request.json | |
| context = data.get('context', '') | |
| try: | |
| questions = llm_handler.generate_questions(context) | |
| return jsonify(questions) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| def generate_final_prompt(): | |
| """ | |
| Generate final prompt based on context, questions, and answers | |
| Request Payload: | |
| { | |
| "context": "Initial context", | |
| "questions": [...], | |
| "answers": {...} | |
| } | |
| Returns: | |
| Generated final prompt | |
| """ | |
| data = request.json | |
| context = data.get('context', '') | |
| questions = data.get('questions', []) | |
| answers = data.get('answers', {}) | |
| try: | |
| final_prompt = llm_handler.generate_final_prompt(context, questions, answers) | |
| return jsonify({"prompt": final_prompt}) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| def process_invitations(): | |
| """ | |
| Process CSV file and generate invitations | |
| Request Parameters: | |
| - file: CSV file | |
| - prompt: Invitation generation prompt | |
| Returns: | |
| Processed CSV file with generated invitations | |
| """ | |
| if 'file' not in request.files: | |
| return jsonify({"error": "No file uploaded"}), 400 | |
| file = request.files['file'] | |
| user_prompt = request.form.get('prompt', '') | |
| if file.filename == '': | |
| return jsonify({"error": "No selected file"}), 400 | |
| try: | |
| # Process CSV and generate invitations | |
| processed_df = process_csv(file, user_prompt) | |
| # Save processed DataFrame to a bytes buffer | |
| output = io.BytesIO() | |
| processed_df.to_csv(output, index=False) | |
| output.seek(0) | |
| # Return the file | |
| return send_file( | |
| output, | |
| mimetype='text/csv', | |
| as_attachment=True, | |
| download_name='generated_invitations.csv' | |
| ) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| if __name__ == '__main__': | |
| # Configurable port, defaults to 5000 | |
| port = int(os.environ.get('PORT', 5000)) | |
| app.run(host='0.0.0.0', port=port, debug=True) |