import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.*; import org.json.JSONObject; import org.json.JSONArray; public class Main { public static void main(String[] args) { if (args.length == 0) { System.out.println("Error: No input received."); return; } String userQuestion = args[0].toLowerCase(); Set keywordsSet = loadKeywords("/content/java_chatbot_keywords.csv"); String response = getResponse(userQuestion, keywordsSet); System.out.println(response); // Correct output for Gradio } public static Set loadKeywords(String filePath) { Set keywordsSet = new HashSet<>(); try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { String line; while ((line = br.readLine()) != null) { String[] parts = line.split(","); if (parts.length >= 2) { keywordsSet.add(parts[0].trim().toLowerCase()); String[] variations = parts[1].split(";"); for (String variation : variations) { keywordsSet.add(variation.trim().toLowerCase()); } } } } catch (IOException e) { System.out.println("Error loading CSV: " + e.getMessage()); } return keywordsSet; } public static String getResponse(String userQuestion, Set keywordsSet) { for (String keyword : keywordsSet) { if (userQuestion.contains(keyword)) { if (!userQuestion.contains("java")) { userQuestion = userQuestion + " Java"; } return fetchResponseFromGemini(userQuestion); } } return "This is not in the syllabus."; } public static String fetchResponseFromGemini(String prompt) { String apiKey = "AIzaSyANMb7mDPE0Ji9eV1hYC_Yzj8IpYoJW4LE"; // Replace with your API key String response = ""; try { URL url = new URL("https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=" + apiKey); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); String jsonBody = "{ \\\"contents\\\": [{ \\\"parts\\\": [{ \\\"text\\\": \\\"" + prompt + "\\\" }]}]}"; try (OutputStream os = connection.getOutputStream()) { os.write(jsonBody.getBytes("UTF-8")); } StringBuilder rawResponse = new StringBuilder(); try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String line; while ((line = br.readLine()) != null) { rawResponse.append(line); } } JSONObject jsonResponse = new JSONObject(rawResponse.toString()); JSONArray candidates = jsonResponse.optJSONArray("candidates"); if (candidates != null && candidates.length() > 0) { JSONObject candidate = candidates.getJSONObject(0); JSONObject content = candidate.optJSONObject("content"); if (content != null) { JSONArray parts = content.optJSONArray("parts"); if (parts != null && parts.length() > 0) { response = parts.getJSONObject(0).getString("text"); } else { response = "No text content available in the parts array."; } } else { response = "No content available in the response."; } } else { response = "No candidates available in the response."; } } catch (Exception e) { return "Error fetching response: " + e.getMessage(); } return response; } }