File size: 5,966 Bytes
b1b2448
 
 
 
 
 
 
b1539d9
b1b2448
 
 
 
 
21e883b
b1b2448
 
b1539d9
b1b2448
 
 
 
 
 
 
 
 
 
21e883b
b1b2448
 
 
 
 
 
 
 
 
 
 
 
 
b1539d9
b1b2448
aca391b
b1539d9
3022b90
 
 
21e883b
3022b90
 
 
 
 
21e883b
3022b90
b1539d9
 
 
 
3022b90
 
21e883b
3022b90
21e883b
3022b90
b1b2448
3022b90
21e883b
b1b2448
b1539d9
 
 
 
 
 
 
 
 
 
 
b1b2448
 
 
 
 
b1539d9
b1b2448
b1539d9
21e883b
 
b1539d9
b1b2448
 
b1539d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1b2448
21e883b
b1539d9
 
 
 
21e883b
 
b1539d9
 
 
 
21e883b
 
 
 
b1539d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21e883b
b1539d9
 
 
 
 
 
 
 
 
 
 
21e883b
b1b2448
21e883b
 
 
 
 
 
 
 
b1539d9
 
 
 
 
 
 
3022b90
b1b2448
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import gradio as gr
import csv
import re
import tempfile
import os
import requests

# Load system prompt from 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"

def score_qa(question, answer):
    """Get score from DeepSeek API"""
    try:
        prompt = ECO_PROMPT.format(question=question, answer=answer)
        
        headers = {
            "Authorization": f"Bearer {DEEPSEEK_API_KEY}",
            "Content-Type": "application/json"
        }

        payload = {
            "model": "deepseek-chat",
            "messages": [{"role": "user", "content": prompt}],
            "temperature": 0.1,
            "max_tokens": 5
        }

        response = requests.post(DEEPSEEK_API_URL, json=payload, headers=headers)
        response.raise_for_status()
        
        output = response.json()['choices'][0]['message']['content']
        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

def judge_ecolinguistics_from_csv(csv_file):
    """Process CSV and generate results"""
    rows = []
    with open(csv_file.name, "r", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        rows = list(reader)
    
    results = []
    total_score = 0

    for r in rows:
        sc = score_qa(r.get("question", ""), r.get("answer", ""))
        total_score += sc
        results.append({
            "question_number": r.get("question_number", ""),
            "score": sc
        })
    
    with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".csv", encoding="utf-8") as out_file:
        writer = csv.DictWriter(out_file, fieldnames=["question_number", "score"])
        writer.writeheader()
        writer.writerows(results)
        writer.writerow({"question_number": "Total", "score": total_score})
        out_path = out_file.name

    percentage = (total_score / (len(rows) * 5)) * 100 if rows else 0.0
    percentage_display = f"""
    <div style="
        padding: 25px;
        background: #f0fff4;
        border-radius: 12px;
        margin: 20px 0;
        text-align: center;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    ">
        <h3 style="color: #22543d; margin: 0; font-size: 1.4em;">
            🌱 Overall Score: <span style="color: #38a169;">{percentage:.1f}%</span>
        </h3>
    </div>
    """

    return out_path, percentage_display

# Custom theme and styling
custom_theme = gr.themes.Default().set(
    body_background_fill="#f8fff9",
    button_primary_background_fill="#38a169",
    button_primary_text_color="#ffffff",
    button_primary_background_fill_hover="#2e7d32",
)

css = """
.gradio-container { max-width: 800px !important; }
#upload-box {
    border: 2px dashed #38a169 !important;
    padding: 30px !important;
    border-radius: 15px !important;
    background: #f8fff9 !important;
    min-height: 150px !important;
}
#upload-box:hover {
    border-color: #2e7d32 !important;
    background: #f0fff4 !important;
}
#download-box {
    border: 2px solid #38a169 !important;
    padding: 20px !important;
    border-radius: 15px !important;
    background: #f8fff9 !important;
}
#logo {
    border-radius: 15px !important;
    border: 2px solid #38a169 !important;
    padding: 5px !important;
    background: white !important;
}
.dark #logo { background: #f0fff4 !important; }
.footer {
    text-align: center;
    padding: 15px !important;
    background: #e8f5e9 !important;
    border-radius: 8px !important;
    margin-top: 25px !important;
}
"""

with gr.Blocks(theme=custom_theme, css=css) as demo:
    # Header Section
    with gr.Row():
        gr.Image("logo.png", 
               show_label=False,
               width=200,
               height=200,
               elem_id="logo")
        
        gr.Markdown("""
        <div style="margin-left: 25px;">
            <h1 style="margin: 0; color: #22543d; font-size: 2.2em;">🌿 EcoLingua</h1>
            <p style="margin: 10px 0 0 0; color: #38a169; font-size: 1.1em;">
                Sustainable Communication Assessment Platform
            </p>
        </div>
        """)

    # Main Content
    with gr.Column(variant="panel"):
        gr.Markdown("""
        ## πŸ“€ Upload Your Q&A CSV
        <div style="
            background: #f0fff4;
            padding: 20px;
            border-radius: 10px;
            margin: 15px 0;
        ">
            <p style="margin: 0 0 10px 0; font-weight: 500;">Required CSV format:</p>
            <div style="
                background: white;
                padding: 15px;
                border-radius: 8px;
                font-family: monospace;
            ">
                question_number,question,answer<br>
                1,"Question text...","Answer text..."<br>
                2,"Another question...","Another answer..."
            </div>
        </div>
        """)
        
        with gr.Row():
            csv_input = gr.File(
                label=" ",
                file_types=[".csv"],
                elem_id="upload-box"
            )
            
            csv_output = gr.File(
                label="Download Results",
                interactive=False,
                elem_id="download-box"
            )
        
        html_output = gr.HTML()
        
        csv_input.change(
            judge_ecolinguistics_from_csv,
            inputs=csv_input,
            outputs=[csv_output, html_output]
        )

    # Footer
    gr.Markdown("""
    <div class="footer">
        <p style="margin: 0; color: #2e7d32; font-size: 0.9em;">
            πŸƒ Powered by DeepSeek AI | Environmentally Conscious Language Analysis πŸƒ
        </p>
    </div>
    """)

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