63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package handler
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"tencent_ocr/pkg/service"
|
||
)
|
||
|
||
type RateHandler struct {
|
||
geminiService *service.GeminiService
|
||
}
|
||
|
||
func NewRateHandler(geminiService *service.GeminiService) *RateHandler {
|
||
return &RateHandler{
|
||
geminiService: geminiService,
|
||
}
|
||
}
|
||
|
||
type RateRequest struct {
|
||
Text string `json:"text" binding:"required"`
|
||
Criteria string `json:"criteria"`
|
||
WritingRequirement string `json:"writing_requirement"`
|
||
}
|
||
|
||
type RateResponse struct {
|
||
Result string `json:"result"`
|
||
Success bool `json:"success"`
|
||
}
|
||
|
||
func (h *RateHandler) HandleRate(c *gin.Context) {
|
||
var req RateRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, RateResponse{
|
||
Success: false,
|
||
Result: "Invalid request format",
|
||
})
|
||
return
|
||
}
|
||
|
||
// Process with Gemini
|
||
criteria := req.Criteria
|
||
if criteria == "" {
|
||
criteria = `你是一名语文老师。你正在给学生的作文打分。根据以下中考作文评分标准,给作文打分。
|
||
## 评分总分值:100分。
|
||
### 88-100分 符合题意;写作目的和对象明确;思考充分,立意深刻,感情真挚;选材精当,内容充实;中心突出,条理清晰;表达准确,语言流畅。
|
||
### 75-87分 符合题意;写作目的和对象较明确;思考较充分,立意清楚,感情真实;选材合理,内容具体;中心明确,有一定条理;表达较准确,语言通畅。
|
||
### 60-74分 符合题意;写作目的和对象较模糊;有一定思考,感情真实;有一定内容;结构基本完整;语言尚通畅。
|
||
### 60分以下 不符合题意;缺乏写作目的和对象;基本没有思考,感情虚假;内容空洞;结构混乱;不成篇。`
|
||
result, err := h.geminiService.ProcessText(c.Request.Context(), req.Text)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, RateResponse{
|
||
Success: false,
|
||
Result: "Text processing failed",
|
||
})
|
||
return
|
||
}
|
||
|
||
c.JSON(http.StatusOK, RateResponse{
|
||
Success: true,
|
||
Result: result,
|
||
})
|
||
} |