Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,24 @@ import os
|
|
| 2 |
import re
|
| 3 |
import json
|
| 4 |
import traceback
|
| 5 |
-
import spaces
|
| 6 |
-
import torch
|
| 7 |
import tempfile
|
| 8 |
import subprocess
|
| 9 |
import wikipedia
|
| 10 |
import gradio as gr
|
| 11 |
import requests
|
| 12 |
import pandas as pd
|
| 13 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 14 |
from ddgs import DDGS
|
|
|
|
| 15 |
|
| 16 |
DEFAULT_API_URL = "https://hf.space"
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
| 24 |
-
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-small", device="cpu")
|
| 25 |
|
| 26 |
def download_file(task_id: str, file_name: str) -> str | None:
|
| 27 |
if not file_name:
|
|
@@ -51,9 +49,17 @@ def read_attached_file(file_path: str) -> str:
|
|
| 51 |
elif ext in [".xlsx", ".xls"]:
|
| 52 |
df = pd.read_excel(file_path)
|
| 53 |
return f"Excel Table Contents:\n{df.to_string(index=False)}"
|
| 54 |
-
elif ext
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
elif ext in [".txt", ".csv", ".json"]:
|
| 58 |
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 59 |
return f.read()[:6000]
|
|
@@ -157,7 +163,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 157 |
valid_items.append(item)
|
| 158 |
prompts.append(user_prompt)
|
| 159 |
|
| 160 |
-
final_answers =
|
| 161 |
|
| 162 |
results_log = []
|
| 163 |
answers_payload = []
|
|
|
|
| 2 |
import re
|
| 3 |
import json
|
| 4 |
import traceback
|
|
|
|
|
|
|
| 5 |
import tempfile
|
| 6 |
import subprocess
|
| 7 |
import wikipedia
|
| 8 |
import gradio as gr
|
| 9 |
import requests
|
| 10 |
import pandas as pd
|
|
|
|
| 11 |
from ddgs import DDGS
|
| 12 |
+
from groq import Groq
|
| 13 |
|
| 14 |
DEFAULT_API_URL = "https://hf.space"
|
| 15 |
+
GROQ_MODEL = "openai/gpt-oss-120b"
|
| 16 |
+
AUDIO_MODEL = "whisper-large-v3"
|
| 17 |
|
| 18 |
+
def get_groq_client():
|
| 19 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 20 |
+
if not api_key:
|
| 21 |
+
raise RuntimeError("GROQ_API_KEY is not configured in Hugging Face Secrets.")
|
| 22 |
+
return Groq(api_key=api_key)
|
|
|
|
| 23 |
|
| 24 |
def download_file(task_id: str, file_name: str) -> str | None:
|
| 25 |
if not file_name:
|
|
|
|
| 49 |
elif ext in [".xlsx", ".xls"]:
|
| 50 |
df = pd.read_excel(file_path)
|
| 51 |
return f"Excel Table Contents:\n{df.to_string(index=False)}"
|
| 52 |
+
elif ext in [".mp3", ".wav", ".m4a", ".ogg", ".webm", ".flac"]:
|
| 53 |
+
client = get_groq_client()
|
| 54 |
+
|
| 55 |
+
with open(file_path, "rb") as audio_file:
|
| 56 |
+
transcript = client.audio.transcriptions.create(
|
| 57 |
+
file=(os.path.basename(file_path), audio_file.read()),
|
| 58 |
+
model=AUDIO_MODEL,
|
| 59 |
+
response_format="text",
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
return f"Audio Transcript ({os.path.basename(file_path)}):\n{transcript}"
|
| 63 |
elif ext in [".txt", ".csv", ".json"]:
|
| 64 |
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 65 |
return f.read()[:6000]
|
|
|
|
| 163 |
valid_items.append(item)
|
| 164 |
prompts.append(user_prompt)
|
| 165 |
|
| 166 |
+
final_answers = generate_answers(prompts)
|
| 167 |
|
| 168 |
results_log = []
|
| 169 |
answers_payload = []
|