Spaces:
Runtime error
Runtime error
File size: 4,292 Bytes
c131ee5 | 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | 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<String> keywordsSet = loadKeywords("/content/java_chatbot_keywords.csv");
String response = getResponse(userQuestion, keywordsSet);
System.out.println(response); // Correct output for Gradio
}
public static Set<String> loadKeywords(String filePath) {
Set<String> 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<String> 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;
}
}
|