paduxx commited on
Commit
6ebaa54
·
1 Parent(s): b09a71d

Initial commit

Browse files
Files changed (1) hide show
  1. index.php +123 -0
index.php ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>CodeLlamaによる問題生成テストサイト</title>
5
+ </head>
6
+ <body>
7
+ <h1>CodeLlamaによる問題生成テストサイト</h1>
8
+
9
+ <?php
10
+
11
+ // Hugging Face APIトークンとAPIエンドポイント
12
+ $apiToken = 'YOUR_HUGGING_FACE_API_TOKEN';
13
+ $modelName = 'elyza/ELYZA-japanese-CodeLlama-7b-instruct'; // 使用するモデル名【変更可能】
14
+
15
+ // TheBloke/CodeLlama-7B-GGUF
16
+ // elyza/ELYZA-japanese-CodeLlama-7b
17
+ // mmnga/ELYZA-japanese-CodeLlama-7b-instruct-gguf
18
+ // codellama/CodeLlama-7b-Instruct-hf
19
+ // bigcode/starcoder
20
+ // Salesforce/codegen-350M-multi
21
+ // APIエンドポイント
22
+ $url = "https://api-inference.huggingface.co/models/$modelName";
23
+
24
+ // フォームがPOSTメソッドで送信されると処理を実行(&空じゃないかチェック)
25
+ if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['inputs'])) {
26
+ $inputs = $_POST['inputs'];
27
+
28
+
29
+ // cURLを使用してリクエストを送信
30
+ // cURLセッションを初期化
31
+ $ch = curl_init($url);
32
+
33
+ // cURLでSSL証明書の検証をスキップする【一時的に】
34
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
35
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
36
+ curl_exec($ch);
37
+ if(curl_errno($ch)) {
38
+ echo 'Error:' . curl_error($ch);
39
+ }
40
+ curl_close($ch);
41
+
42
+
43
+ // cURLの状態確認
44
+ if ($ch) {
45
+ echo "cURLは使えます。";
46
+ curl_close($ch);
47
+ } else {
48
+ echo "Failed to initialize cURL.";
49
+ }
50
+
51
+ // APIのURLを設定
52
+ curl_setopt($ch, CURLOPT_URL, $url);
53
+ // API応答を文字列で返す設定
54
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
55
+ // POSTリクエストを指定
56
+ curl_setopt($ch, CURLOPT_POST, true);
57
+ // HTTPヘッダー(APIトークン、コンテンツタイプを指定)
58
+ curl_setopt($ch, CURLOPT_HTTPHEADER, [
59
+ 'Authorization: Bearer ' . $apiToken,
60
+ 'Content-Type: application/json'
61
+ ]);
62
+
63
+
64
+ // 【後で変更】フォーム入力とプロンプトのテンプレートを、一部を変数化し、組み合わせる
65
+ echo "APIに送信する文章\n";
66
+
67
+
68
+
69
+ // APIに送信するデータをJSON形式で指定
70
+ curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
71
+ "inputs" => $inputs,
72
+ // 生成テキストの最大長
73
+ "max_tokens" => 10000,
74
+ // パラメータ類
75
+ "temperature" => 0.3 // 応答のランダム性
76
+ ]));
77
+
78
+
79
+
80
+ // cURLリクエストを実行、APIの応答を変数responseに取得
81
+ echo $response = curl_exec($ch);
82
+ ini_set('max_execution_time', '300'); // 最大実行時間の設定(5分)
83
+
84
+
85
+ // cURLエラーが発生した場合のチェック
86
+ if (curl_errno($ch)) {
87
+ echo "<div class='response'>Error: " . curl_error($ch) . "</div>";
88
+ } else {
89
+ // 【JSON形式のAPI応答を連想配列に変換(?)】
90
+ $result = json_decode($response, true);
91
+ // 整形用処理
92
+ $response = str_replace('[{"generated_text":"', PHP_EOL, $response);
93
+ $response = str_replace('"}]', PHP_EOL, $response);
94
+ $response = str_replace('\n', PHP_EOL, $response);
95
+ $response = str_replace('\r', PHP_EOL, $response);
96
+ $response = str_replace('\t', PHP_EOL, $response);
97
+ }
98
+ // cURLセッションを終了し、リソースを開放
99
+ curl_close($ch);
100
+ }
101
+
102
+ ?>
103
+
104
+
105
+
106
+ <!-- 問題を表示する部分 -->
107
+ <h2>生成文章(問題文の表示ウィンドウにする予定)</h2>
108
+ <?php
109
+ echo "<textarea id=\"response\" readonly rows=\"30\" cols=\"80\" style=\"font-size: 20px;\">" . htmlspecialchars($response) . "</textarea>";
110
+ ?>
111
+
112
+
113
+ <!-- 出力推定問題における回答フォームの部分 -->
114
+ <div class="container">
115
+ <form method="POST">
116
+ <label for="inputs">APIに送信する文章:</label>
117
+ <textarea id="inputs" name="inputs" rows="20" cols="40" style="font-size: 25px;"required></textarea>
118
+ <button type="submit">Submit</button>
119
+ </form>
120
+
121
+
122
+ </body>
123
+ </html>