Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import tempfile
|
|
5 |
import os
|
6 |
import requests
|
7 |
|
8 |
-
#
|
9 |
with open("system_instructions.txt", "r", encoding="utf-8") as f:
|
10 |
ECO_PROMPT = f.read()
|
11 |
|
@@ -14,7 +14,7 @@ DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY")
|
|
14 |
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
|
15 |
|
16 |
def score_qa(question, answer):
|
17 |
-
"""
|
18 |
try:
|
19 |
prompt = ECO_PROMPT.format(question=question, answer=answer)
|
20 |
|
@@ -39,10 +39,10 @@ def score_qa(question, answer):
|
|
39 |
|
40 |
except Exception as e:
|
41 |
print(f"API Error: {str(e)}")
|
42 |
-
return 1
|
43 |
|
44 |
def judge_ecolinguistics_from_csv(csv_file):
|
45 |
-
"""Process CSV and
|
46 |
rows = []
|
47 |
with open(csv_file.name, "r", encoding="utf-8") as f:
|
48 |
reader = csv.DictReader(f)
|
@@ -54,7 +54,10 @@ def judge_ecolinguistics_from_csv(csv_file):
|
|
54 |
for r in rows:
|
55 |
sc = score_qa(r.get("question", ""), r.get("answer", ""))
|
56 |
total_score += sc
|
57 |
-
results.append({
|
|
|
|
|
|
|
58 |
|
59 |
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".csv", encoding="utf-8") as out_file:
|
60 |
writer = csv.DictWriter(out_file, fieldnames=["question_number", "score"])
|
@@ -65,45 +68,119 @@ def judge_ecolinguistics_from_csv(csv_file):
|
|
65 |
|
66 |
percentage = (total_score / (len(rows) * 5)) * 100 if rows else 0.0
|
67 |
percentage_display = f"""
|
68 |
-
<div style="
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
</div>
|
71 |
"""
|
72 |
|
73 |
return out_path, percentage_display
|
74 |
|
75 |
-
# Custom theme
|
76 |
custom_theme = gr.themes.Default().set(
|
77 |
-
body_background_fill="#
|
78 |
button_primary_background_fill="#38a169",
|
79 |
button_primary_text_color="#ffffff",
|
|
|
80 |
)
|
81 |
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
with gr.Row():
|
85 |
gr.Image("logo.png",
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
show_download_button=False)
|
91 |
|
92 |
gr.Markdown("""
|
93 |
-
<div style="margin-left:
|
94 |
-
<h1 style="margin
|
95 |
-
<p style="margin
|
96 |
-
Sustainable Communication
|
97 |
</p>
|
98 |
</div>
|
99 |
""")
|
100 |
|
101 |
-
# Main
|
102 |
-
with gr.Column():
|
103 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
with gr.Row():
|
105 |
-
csv_input = gr.File(
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
html_output = gr.HTML()
|
109 |
|
@@ -114,7 +191,13 @@ with gr.Blocks(theme=custom_theme) as demo:
|
|
114 |
)
|
115 |
|
116 |
# Footer
|
117 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
if __name__ == "__main__":
|
120 |
demo.launch()
|
|
|
5 |
import os
|
6 |
import requests
|
7 |
|
8 |
+
# Load system prompt from file
|
9 |
with open("system_instructions.txt", "r", encoding="utf-8") as f:
|
10 |
ECO_PROMPT = f.read()
|
11 |
|
|
|
14 |
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
|
15 |
|
16 |
def score_qa(question, answer):
|
17 |
+
"""Get score from DeepSeek API"""
|
18 |
try:
|
19 |
prompt = ECO_PROMPT.format(question=question, answer=answer)
|
20 |
|
|
|
39 |
|
40 |
except Exception as e:
|
41 |
print(f"API Error: {str(e)}")
|
42 |
+
return 1 # Fallback score
|
43 |
|
44 |
def judge_ecolinguistics_from_csv(csv_file):
|
45 |
+
"""Process CSV and generate results"""
|
46 |
rows = []
|
47 |
with open(csv_file.name, "r", encoding="utf-8") as f:
|
48 |
reader = csv.DictReader(f)
|
|
|
54 |
for r in rows:
|
55 |
sc = score_qa(r.get("question", ""), r.get("answer", ""))
|
56 |
total_score += sc
|
57 |
+
results.append({
|
58 |
+
"question_number": r.get("question_number", ""),
|
59 |
+
"score": sc
|
60 |
+
})
|
61 |
|
62 |
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".csv", encoding="utf-8") as out_file:
|
63 |
writer = csv.DictWriter(out_file, fieldnames=["question_number", "score"])
|
|
|
68 |
|
69 |
percentage = (total_score / (len(rows) * 5)) * 100 if rows else 0.0
|
70 |
percentage_display = f"""
|
71 |
+
<div style="
|
72 |
+
padding: 25px;
|
73 |
+
background: #f0fff4;
|
74 |
+
border-radius: 12px;
|
75 |
+
margin: 20px 0;
|
76 |
+
text-align: center;
|
77 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
78 |
+
">
|
79 |
+
<h3 style="color: #22543d; margin: 0; font-size: 1.4em;">
|
80 |
+
π± Overall Score: <span style="color: #38a169;">{percentage:.1f}%</span>
|
81 |
+
</h3>
|
82 |
</div>
|
83 |
"""
|
84 |
|
85 |
return out_path, percentage_display
|
86 |
|
87 |
+
# Custom theme and styling
|
88 |
custom_theme = gr.themes.Default().set(
|
89 |
+
body_background_fill="#f8fff9",
|
90 |
button_primary_background_fill="#38a169",
|
91 |
button_primary_text_color="#ffffff",
|
92 |
+
button_primary_background_fill_hover="#2e7d32",
|
93 |
)
|
94 |
|
95 |
+
css = """
|
96 |
+
.gradio-container { max-width: 800px !important; }
|
97 |
+
#upload-box {
|
98 |
+
border: 2px dashed #38a169 !important;
|
99 |
+
padding: 30px !important;
|
100 |
+
border-radius: 15px !important;
|
101 |
+
background: #f8fff9 !important;
|
102 |
+
min-height: 150px !important;
|
103 |
+
}
|
104 |
+
#upload-box:hover {
|
105 |
+
border-color: #2e7d32 !important;
|
106 |
+
background: #f0fff4 !important;
|
107 |
+
}
|
108 |
+
#download-box {
|
109 |
+
border: 2px solid #38a169 !important;
|
110 |
+
padding: 20px !important;
|
111 |
+
border-radius: 15px !important;
|
112 |
+
background: #f8fff9 !important;
|
113 |
+
}
|
114 |
+
#logo {
|
115 |
+
border-radius: 15px !important;
|
116 |
+
border: 2px solid #38a169 !important;
|
117 |
+
padding: 5px !important;
|
118 |
+
background: white !important;
|
119 |
+
}
|
120 |
+
.dark #logo { background: #f0fff4 !important; }
|
121 |
+
.footer {
|
122 |
+
text-align: center;
|
123 |
+
padding: 15px !important;
|
124 |
+
background: #e8f5e9 !important;
|
125 |
+
border-radius: 8px !important;
|
126 |
+
margin-top: 25px !important;
|
127 |
+
}
|
128 |
+
"""
|
129 |
+
|
130 |
+
with gr.Blocks(theme=custom_theme, css=css) as demo:
|
131 |
+
# Header Section
|
132 |
with gr.Row():
|
133 |
gr.Image("logo.png",
|
134 |
+
show_label=False,
|
135 |
+
width=200,
|
136 |
+
height=200,
|
137 |
+
elem_id="logo")
|
|
|
138 |
|
139 |
gr.Markdown("""
|
140 |
+
<div style="margin-left: 25px;">
|
141 |
+
<h1 style="margin: 0; color: #22543d; font-size: 2.2em;">πΏ EcoLingua</h1>
|
142 |
+
<p style="margin: 10px 0 0 0; color: #38a169; font-size: 1.1em;">
|
143 |
+
Sustainable Communication Assessment Platform
|
144 |
</p>
|
145 |
</div>
|
146 |
""")
|
147 |
|
148 |
+
# Main Content
|
149 |
+
with gr.Column(variant="panel"):
|
150 |
+
gr.Markdown("""
|
151 |
+
## π€ Upload Your Q&A CSV
|
152 |
+
<div style="
|
153 |
+
background: #f0fff4;
|
154 |
+
padding: 20px;
|
155 |
+
border-radius: 10px;
|
156 |
+
margin: 15px 0;
|
157 |
+
">
|
158 |
+
<p style="margin: 0 0 10px 0; font-weight: 500;">Required CSV format:</p>
|
159 |
+
<div style="
|
160 |
+
background: white;
|
161 |
+
padding: 15px;
|
162 |
+
border-radius: 8px;
|
163 |
+
font-family: monospace;
|
164 |
+
">
|
165 |
+
question_number,question,answer<br>
|
166 |
+
1,"Question text...","Answer text..."<br>
|
167 |
+
2,"Another question...","Another answer..."
|
168 |
+
</div>
|
169 |
+
</div>
|
170 |
+
""")
|
171 |
+
|
172 |
with gr.Row():
|
173 |
+
csv_input = gr.File(
|
174 |
+
label=" ",
|
175 |
+
file_types=[".csv"],
|
176 |
+
elem_id="upload-box"
|
177 |
+
)
|
178 |
+
|
179 |
+
csv_output = gr.File(
|
180 |
+
label="Download Results",
|
181 |
+
interactive=False,
|
182 |
+
elem_id="download-box"
|
183 |
+
)
|
184 |
|
185 |
html_output = gr.HTML()
|
186 |
|
|
|
191 |
)
|
192 |
|
193 |
# Footer
|
194 |
+
gr.Markdown("""
|
195 |
+
<div class="footer">
|
196 |
+
<p style="margin: 0; color: #2e7d32; font-size: 0.9em;">
|
197 |
+
π Powered by DeepSeek AI | Environmentally Conscious Language Analysis π
|
198 |
+
</p>
|
199 |
+
</div>
|
200 |
+
""")
|
201 |
|
202 |
if __name__ == "__main__":
|
203 |
demo.launch()
|