Download Main.java from anithai/java_chat: direct link, hf CLI and curl.
- Browser
- Download file 4.19 kB
-
https://huggingface.co/spaces/anithai/java_chat/resolve/9ee0edf3b0912cf08e8aae38570966919aa4877e/Main.java
- Command line
-
hf download hf://spaces/anithai/java_chat@9ee0edf3b0912cf08e8aae38570966919aa4877e/Main.java
-
curl -L -o Main.java https://huggingface.co/spaces/anithai/java_chat/resolve/9ee0edf3b0912cf08e8aae38570966919aa4877e/Main.java
4.19 kB
| 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; | |
| } | |
| } | |