anithai commited on
Commit
47dd35c
·
verified ·
1 Parent(s): 4227052

Main.java

Browse files
Files changed (1) hide show
  1. Main.java +0 -103
Main.java DELETED
@@ -1,103 +0,0 @@
1
- import java.io.*;
2
- import java.net.HttpURLConnection;
3
- import java.net.URL;
4
- import java.util.*;
5
- import org.json.JSONObject;
6
- import org.json.JSONArray;
7
-
8
- public class Main {
9
- public static void main(String[] args) {
10
- if (args.length == 0) {
11
- System.out.println("Error: No input received.");
12
- return;
13
- }
14
-
15
- String userQuestion = args[0].toLowerCase();
16
- Set<String> keywordsSet = loadKeywords("/content/java_chatbot_keywords.csv");
17
-
18
- String response = getResponse(userQuestion, keywordsSet);
19
- System.out.println(response); // Correct output for Gradio
20
- }
21
-
22
- public static Set<String> loadKeywords(String filePath) {
23
- Set<String> keywordsSet = new HashSet<>();
24
- try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
25
- String line;
26
- while ((line = br.readLine()) != null) {
27
- String[] parts = line.split(",");
28
- if (parts.length >= 2) {
29
- keywordsSet.add(parts[0].trim().toLowerCase());
30
- String[] variations = parts[1].split(";");
31
- for (String variation : variations) {
32
- keywordsSet.add(variation.trim().toLowerCase());
33
- }
34
- }
35
- }
36
- } catch (IOException e) {
37
- System.out.println("Error loading CSV: " + e.getMessage());
38
- }
39
- return keywordsSet;
40
- }
41
-
42
- public static String getResponse(String userQuestion, Set<String> keywordsSet) {
43
- for (String keyword : keywordsSet) {
44
- if (userQuestion.contains(keyword)) {
45
- if (!userQuestion.contains("java")) {
46
- userQuestion = userQuestion + " Java";
47
- }
48
- return fetchResponseFromGemini(userQuestion);
49
- }
50
- }
51
- return "This is not in the syllabus.";
52
- }
53
-
54
- public static String fetchResponseFromGemini(String prompt) {
55
- String apiKey = "AIzaSyANMb7mDPE0Ji9eV1hYC_Yzj8IpYoJW4LE"; // Replace with your API key
56
- String response = "";
57
- try {
58
- URL url = new URL("https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=" + apiKey);
59
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
60
- connection.setRequestMethod("POST");
61
- connection.setRequestProperty("Content-Type", "application/json");
62
- connection.setDoOutput(true);
63
-
64
- String jsonBody = "{ \\\"contents\\\": [{ \\\"parts\\\": [{ \\\"text\\\": \\\"" + prompt + "\\\" }]}]}";
65
- try (OutputStream os = connection.getOutputStream()) {
66
- os.write(jsonBody.getBytes("UTF-8"));
67
- }
68
-
69
- StringBuilder rawResponse = new StringBuilder();
70
- try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
71
- String line;
72
- while ((line = br.readLine()) != null) {
73
- rawResponse.append(line);
74
- }
75
- }
76
-
77
- JSONObject jsonResponse = new JSONObject(rawResponse.toString());
78
- JSONArray candidates = jsonResponse.optJSONArray("candidates");
79
-
80
- if (candidates != null && candidates.length() > 0) {
81
- JSONObject candidate = candidates.getJSONObject(0);
82
- JSONObject content = candidate.optJSONObject("content");
83
-
84
- if (content != null) {
85
- JSONArray parts = content.optJSONArray("parts");
86
- if (parts != null && parts.length() > 0) {
87
- response = parts.getJSONObject(0).getString("text");
88
- } else {
89
- response = "No text content available in the parts array.";
90
- }
91
- } else {
92
- response = "No content available in the response.";
93
- }
94
- } else {
95
- response = "No candidates available in the response.";
96
- }
97
- } catch (Exception e) {
98
- return "Error fetching response: " + e.getMessage();
99
- }
100
- return response;
101
- }
102
- }
103
-