File size: 2,558 Bytes
1cb836d
aca391b
 
 
 
57f5984
1cb836d
57f5984
 
 
1cb836d
57f5984
 
 
aca391b
 
57f5984
 
 
 
 
 
 
 
 
1cb836d
57f5984
 
 
 
 
 
 
 
 
8fc19af
57f5984
 
 
 
 
 
 
 
 
8fc19af
57f5984
 
 
aca391b
 
57f5984
 
 
 
 
aca391b
57f5984
8fc19af
57f5984
1cb836d
aca391b
57f5984
 
 
 
 
a7ddeb5
8fc19af
57f5984
 
 
8fc19af
1cb836d
 
 
57f5984
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
import gradio as gr
import csv
import re
import tempfile
import os
import requests

# 1. Load system prompt from a file
with open("system_instructions.txt", "r", encoding="utf-8") as f:
    ECO_PROMPT = f.read()

# DeepSeek API configuration
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY")
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"  # Verify actual API endpoint

def score_qa(question, answer):
    """Query DeepSeek API to get a score for Q&A pair"""
    try:
        # Format the prompt using our template
        prompt = ECO_PROMPT.format(question=question, answer=answer)
        
        headers = {
            "Authorization": f"Bearer {DEEPSEEK_API_KEY}",
            "Content-Type": "application/json"
        }

        payload = {
            "model": "deepseek-chat",  # Verify correct model name
            "messages": [{
                "role": "user",
                "content": prompt
            }],
            "temperature": 0.1,  # More deterministic output for scoring
            "max_tokens": 5
        }

        response = requests.post(DEEPSEEK_API_URL, json=payload, headers=headers)
        response.raise_for_status()
        
        # Parse response (adjust based on actual API response structure)
        output = response.json()['choices'][0]['message']['content']
        
        # Extract score using same logic as before
        match = re.search(r"\d+", output)
        return int(match.group(0)) if match else 1

    except Exception as e:
        print(f"API Error: {str(e)}")
        return 1  # Fallback score on error

def judge_ecolinguistics_from_csv(csv_file):
    """Existing CSV processing function remains the same"""
    # [Keep the existing implementation exactly as you have it]
    # ... (same file processing logic)
    # ... (same CSV writing logic)
    # ... (same percentage calculation)

    return out_path, percentage_display

# [Keep the Gradio interface configuration exactly as is]
demo = gr.Interface(
    fn=judge_ecolinguistics_from_csv,
    inputs=gr.File(label="Upload CSV with question_number, question, answer"),
    outputs=[
        gr.File(label="Download scored CSV"),
        gr.HTML(label="Percentage Score")
    ],
    title="Ecolinguistics Q&A Scoring",
    description=(
        "Upload a CSV with columns [question_number, question, answer]. "
        "DeepSeek scores each answer from 0–5, then shows a final "
        "percentage score. A detailed CSV with individual scores is provided."
    )
)

if __name__ == "__main__":
    demo.launch()