neovalle's picture
Update app.py
57f5984 verified
raw
history blame
2.56 kB
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()