Spaces:
Runtime error
Runtime error
File size: 7,282 Bytes
1e620c1 6f3256c 015c12b 957d1ee 6f3256c 8323d3a 1e620c1 015c12b 957d1ee 1e620c1 8323d3a 1e620c1 015c12b 1e620c1 957d1ee 8323d3a 957d1ee 1e620c1 8323d3a 1e620c1 8323d3a 1e620c1 8323d3a 1e620c1 8323d3a 1e620c1 8323d3a 1e620c1 8323d3a 1e620c1 8323d3a 1e620c1 6f3256c 015c12b 1e620c1 8323d3a 1e620c1 8323d3a 1e620c1 8323d3a 1e620c1 8323d3a 1e620c1 957d1ee 1e620c1 015c12b 957d1ee 015c12b 957d1ee 1e620c1 957d1ee 015c12b 957d1ee 015c12b 957d1ee 1e620c1 6f3256c 015c12b 1e620c1 6f3256c 957d1ee 015c12b 1e620c1 957d1ee 1e620c1 36ed51a 31243f4 1e620c1 957d1ee 8323d3a 1e620c1 957d1ee 1e620c1 957d1ee 8323d3a 1e620c1 957d1ee 1e620c1 e80aab9 1e620c1 7e4a06b 1e620c1 e80aab9 957d1ee |
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 |
# app.py β handles images, PDFs, text/code, Excel, audio, etc.
import os, json, time, io, tempfile, mimetypes
from functools import lru_cache
import gradio as gr
import requests
import pandas as pd
from openai import OpenAI, RateLimitError, APIError
from duckduckgo_search import DDGS
from PyPDF2 import PdfReader
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
OPENAI_MODEL = "gpt-4o-mini"
TEXT_LIMIT = 8_000
PDF_PAGES = 3
AUDIO_SIZE_CAP = 16 * 1024 * 1024 # 16 MB
# βββββββββββββββ helpers βββββββββββββββ
def duckduckgo_search(query: str, max_results: int = 5) -> str:
with DDGS() as ddgs:
hits = [f"- {r['title']} β {r['href']}"
for r in ddgs.text(query, max_results=max_results)]
return "\n".join(hits) or "No results found."
DDG_SCHEMA = {
"name": "duckduckgo_search",
"description": "Search the web for up-to-date info.",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"},
"max_results": {"type": "integer", "default": 5},
},
"required": ["query"],
},
}
def download_bytes(url: str, cap: int | None = None) -> bytes:
r = requests.get(url, timeout=20)
r.raise_for_status()
data = r.content
if cap and len(data) > cap:
raise ValueError("File too large")
return data
def extract_text_file(url: str) -> str:
try:
txt = download_bytes(url).decode(errors="replace")
return txt[:TEXT_LIMIT]
except Exception as e:
return f"[Could not fetch text file: {e}]"
def extract_pdf(url: str) -> str:
try:
reader = PdfReader(io.BytesIO(download_bytes(url)))
pages = [reader.pages[i].extract_text() or "" for i in range(min(PDF_PAGES, len(reader.pages)))]
return ("\n\n".join(pages))[:TEXT_LIMIT]
except Exception as e:
return f"[Could not read PDF: {e}]"
def extract_excel(url: str) -> str:
try:
buf = io.BytesIO(download_bytes(url))
df = pd.read_excel(buf, nrows=15, engine="openpyxl")
return df.to_csv(index=False, header=True)[:TEXT_LIMIT]
except Exception as e:
return f"[Could not read Excel: {e}]"
def transcribe_audio(url: str, client: OpenAI) -> str:
try:
data = download_bytes(url, cap=AUDIO_SIZE_CAP)
with tempfile.NamedTemporaryFile(delete=False, suffix=".audio") as tmp:
tmp.write(data); tmp.flush()
tr = client.audio.transcriptions.create(model="whisper-1", file=open(tmp.name, "rb"))
return tr.text[:2000]
except Exception as e:
return f"[Could not transcribe audio: {e}]"
# βββββββββββββββ Agent βββββββββββββββ
class GPT4oMiniAgent:
def __init__(self, retries=3, backoff=2.0):
key = os.getenv("OPENAI_API_KEY")
if not key:
raise EnvironmentError("Add OPENAI_API_KEY in Space Secrets")
self.client, self.retries, self.backoff = OpenAI(api_key=key), retries, backoff
self.system_prompt = (
"You are a concise, accurate assistant. If certain, answer directly; "
"if not, call duckduckgo_search first."
)
@lru_cache(maxsize=512)
def __call__(self, question: str, file_url: str | None = None) -> str:
user_parts = [{"type": "text", "text": question}]
if file_url:
ext = os.path.splitext(file_url.split("?")[0].split("#")[0])[1].lower()
if ext in {".png", ".jpg", ".jpeg", ".gif", ".webp"}:
user_parts.append({"type": "image_url", "image_url": {"url": file_url}})
elif ext in {".pdf"}:
user_parts.append({"type": "text", "text": "(PDF extract)\n" + extract_pdf(file_url)})
elif ext in {".xls", ".xlsx"}:
user_parts.append({"type": "text", "text": "(Excel preview)\n" + extract_excel(file_url)})
elif ext in {".txt", ".py", ".md", ".json", ".csv", ".html"}:
user_parts.append({"type": "text", "text": "(File content)\n" + extract_text_file(file_url)})
elif ext in {".mp3", ".wav", ".m4a", ".flac", ".ogg"}:
user_parts.append({"type": "text", "text": "(Audio transcript)\n" + transcribe_audio(file_url, self.client)})
else:
user_parts.append({"type": "text", "text": f"[File available: {file_url}]"} )
msgs = [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": user_parts},
]
resp = self._chat(msgs, tools=[DDG_SCHEMA], tool_choice="auto")
if resp.choices[0].message.tool_calls:
for call in resp.choices[0].message.tool_calls:
args = json.loads(call.function.arguments or "{}")
search_out = duckduckgo_search(**args)
msgs.append({"role": "tool", "tool_call_id": call.id, "name": call.function.name, "content": search_out})
resp = self._chat(msgs)
return resp.choices[0].message.content.strip()
def _chat(self, messages, **kw):
for i in range(1, self.retries + 1):
try:
return self.client.chat.completions.create(
model=OPENAI_MODEL, messages=messages,
temperature=0.0, max_tokens=512, **kw
)
except (RateLimitError, APIError):
time.sleep(self.backoff * i)
raise RuntimeError("OpenAI API failed after retries.")
# βββββββββββββββ pipeline βββββββββββββββ
def run_and_submit_all(profile: gr.OAuthProfile | None):
if not profile:
return "Please log in β", None
username = profile.username
agent = GPT4oMiniAgent()
space_id = os.getenv("SPACE_ID", "local")
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()
rows, answers = [], []
for q in questions:
qid = q["task_id"]
qtext = q["question"]
fileu = q.get("filename") or q.get("file_url")
ans = agent(qtext, fileu)
answers.append({"task_id": qid, "submitted_answer": ans})
rows.append({"Task ID": qid, "Question": qtext, "File": fileu or "", "Answer": ans})
payload = {"username": username, "agent_code": agent_code, "answers": answers}
res = requests.post(f"{DEFAULT_API_URL}/submit", json=payload, timeout=60).json()
status = f"Score {res['score']} % ({res['correct_count']}/{res['total_attempted']})"
return status, pd.DataFrame(rows)
# βββββββββββββββ UI βββββββββββββββ
with gr.Blocks() as demo:
gr.Markdown("# Unit-4 Agent β images, PDFs, Excel, audio, text, etc.")
gr.LoginButton()
run = gr.Button("Run Evaluation & Submit All Answers")
out_status = gr.Textbox(label="Status", interactive=False)
out_table = gr.DataFrame(label="Log", wrap=True)
run.click(run_and_submit_all, outputs=[out_status, out_table])
if __name__ == "__main__":
demo.launch(debug=True, share=False)
|