Spaces:
Build error
Build error
File size: 6,476 Bytes
b1b2448 b1539d9 b1b2448 282332d b1b2448 282332d b1b2448 282332d b1b2448 282332d b1b2448 282332d b1b2448 282332d b1b2448 282332d b1b2448 b1539d9 b1b2448 aca391b 282332d 3022b90 21e883b 3022b90 21e883b 3022b90 b1539d9 3022b90 21e883b 3022b90 21e883b 3022b90 b1b2448 3022b90 21e883b b1b2448 b1539d9 b1b2448 282332d b1b2448 b1539d9 21e883b b1539d9 b1b2448 b1539d9 b1b2448 21e883b b1539d9 21e883b b1539d9 21e883b b1539d9 21e883b b1539d9 21e883b b1b2448 21e883b b1539d9 282332d 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 204 205 206 207 208 209 210 211 212 213 214 |
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()
# Hugging Face configuration
HF_API_KEY = os.environ.get("HF_API_KEY")
HF_API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
def format_llama3_prompt(system_prompt, question, answer):
"""Format prompt according to Llama3's chat template"""
return f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
{system_prompt}<|eot_id|><|start_header_id|>user<|end_header_id|>
Question: {question}
Answer: {answer}
Please provide a numerical score between 1-5 based on the guidelines.<|eot_id|><|start_header_id|>assistant<|end_header_id|>
"""
def score_qa(question, answer):
"""Get score from Llama3 via Hugging Face API"""
try:
prompt = format_llama3_prompt(ECO_PROMPT, question, answer)
headers = {
"Authorization": f"Bearer {HF_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"inputs": prompt,
"parameters": {
"max_new_tokens": 5,
"temperature": 0.1,
"return_full_text": False
}
}
response = requests.post(HF_API_URL, json=payload, headers=headers)
response.raise_for_status()
output = response.json()[0]['generated_text']
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 (unchanged from original)"""
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 (unchanged from original)
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 Meta Llama3 | Environmentally Conscious Language Analysis π
</p>
</div>
""")
if __name__ == "__main__":
demo.launch() |