Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| import openai | |
| import sys | |
| import os | |
| import csv | |
| import json | |
| import lookup | |
| import gpt | |
| openai.api_key = "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" | |
| if not openai.api_key: | |
| openai.api_key = input("Please enter your OpenAI API key: ") | |
| print() | |
| program_name = sys.argv.pop(0) | |
| # CSV processing | |
| csv_file_path = "LIR.csv" # Update with the correct path | |
| with open(csv_file_path, newline='', encoding='utf-8') as csvfile: | |
| reader = csv.DictReader(csvfile) | |
| rows = list(reader) | |
| # Configuration Parameters | |
| chunk_size = 4000 | |
| overlap = 1000 | |
| limit = 20 # Change to 3 to get the top 3 answers | |
| gpt.model = "gpt-3.5-turbo" | |
| # Chunking CSV text | |
| chunks = [row['texte'][i:i + chunk_size] for row in rows for i in range(0, len(row['texte']), chunk_size)] | |
| print("Chunking CSV...\n") | |
| def ask_question(question): | |
| keywords = gpt.get_keywords(question) | |
| matches = lookup.find_matches(chunks, keywords) | |
| top_matches = list(matches.keys())[:limit] | |
| responses = [] | |
| for i, chunk_id in enumerate(top_matches): | |
| chunk = chunks[chunk_id] | |
| response = gpt.answer_question(chunk, question) | |
| if response.get("answer_found"): | |
| matched_row = rows[chunk_id] | |
| # Extract specific properties from the matched row | |
| answer = response.get("response") | |
| # Loop through the columns and add them to the JSON object | |
| json_object = {"GPT_Response": answer} | |
| for column_name, column_value in matched_row.items(): | |
| json_object[column_name] = column_value.encode("utf-8").decode("utf-8") | |
| responses.append(json_object) | |
| responses.append({"keywords:": keywords}) | |
| if not any(response.get("answer_found") for chunk_id in top_matches): | |
| responses.append({"GPT_Response": "I'm sorry, but I can't find that information"}) | |
| return responses | |