LN34's picture
Update app.py
58ebe8b verified
Raw History Blame Contribute Delete
1.62 kB
import os
import gradio as gr
from groq import Groq
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
raise ValueError("Missing GROQ_API_KEY. Please set it in Hugging Face Space Settings → Secrets.")
client = Groq(api_key=api_key)
def summarize_abstract(text):
try:
response = client.chat.completions.create(
model="openai/gpt-oss-120b", # replace with a valid model from client.models.list()
messages=[
{"role": "system", "content": "Summarize the abstract into two parts: Main Idea and Findings."},
{"role": "user", "content": text}
],
max_tokens=250
)
summary = response.choices[0].message.content
# Simple parsing: look for "Main Idea:" and "Findings:"
main_html, findings_html = "", ""
for line in summary.split("\n"):
if line.lower().startswith("main idea"):
main_html = f"<span style='color:pink; font-weight:bold'>{line}</span>"
elif line.lower().startswith("findings"):
findings_html = f"<span style='color:gold; font-weight:bold'>{line}</span>"
return main_html + "<br>" + findings_html if (main_html or findings_html) else summary
except Exception as e:
return f"<span style='color:red'>Error: {str(e)}</span>"
demo = gr.Interface(
fn=summarize_abstract,
inputs=gr.Textbox(lines=10, label="Paste Abstract"),
outputs=gr.HTML(label="Summary"),
title="📄 Scientific Abstract Summarizer",
description="Main Idea appears in pink, Findings in yellow."
)
demo.launch()