Spaces:
Sleeping
Sleeping
Download summarize.py from KhwaishK/ChartTable-RAG: direct link, hf CLI and curl.
- Browser
- Download file 3.25 kB
-
https://huggingface.co/spaces/KhwaishK/ChartTable-RAG/resolve/main/summarize.py
- Command line
-
hf download hf://spaces/KhwaishK/ChartTable-RAG/summarize.py
-
curl -L -o summarize.py https://huggingface.co/spaces/KhwaishK/ChartTable-RAG/resolve/main/summarize.py
3.25 kB
| import time | |
| from langchain_groq import ChatGroq | |
| from langchain_core.prompts import ChatPromptTemplate | |
| from langchain_core.output_parsers import StrOutputParser | |
| def create_text_and_table_summaries(text_chunks, tables): | |
| prompt_text = """ | |
| You are an assistant tasked with summarizing tables and text. | |
| Give a concise summary of the table or text. | |
| Respond only with the summary, no additionnal comment. | |
| Do not start your message by saying "Here is a summary" or anything like that. | |
| Just give the summary as it is. | |
| Table or text chunk: {element} | |
| """ | |
| prompt = ChatPromptTemplate.from_template(prompt_text) | |
| # Summary chain | |
| model = ChatGroq(temperature= 0.5, model="openai/gpt-oss-20b") | |
| summarize_chain = prompt | model | StrOutputParser() | |
| # Sequential processing to prevent 429 errors | |
| text_summaries = [] | |
| print(f"⏳ Summarizing {len(text_chunks)} text chunks...") | |
| for chunk in text_chunks: | |
| text_summaries.append(summarize_chain.invoke(chunk)) | |
| time.sleep(2.0) # Pause to stay under token limits | |
| table_summaries = [] | |
| tables_html = [table.metadata.text_as_html for table in tables] | |
| print(f"⏳ Summarizing {len(tables)} tables...") | |
| for html in tables_html: | |
| table_summaries.append(summarize_chain.invoke(html)) | |
| time.sleep(2.0) | |
| # # Summarize text | |
| # text_summaries = summarize_chain.batch(text_chunks, {"max_concurrency": 3}) | |
| # # Summarize tables | |
| # tables_html = [table.metadata.text_as_html for table in tables] | |
| # table_summaries = summarize_chain.batch(tables_html, {"max_concurrency": 3}) | |
| return text_summaries, table_summaries | |
| def create_image_summaries(images): | |
| prompt_template = """Describe the image in detail. For context, | |
| the image is part of a research paper explaining the transformers | |
| architecture. Be specific about graphs, such as bar plots.""" | |
| messages = [ | |
| ( | |
| "user", | |
| [ | |
| {"type": "text", "text": prompt_template}, | |
| { | |
| "type": "image_url", | |
| "image_url": {"url": "data:image/jpeg;base64,{image}"}, | |
| }, | |
| ], | |
| ) | |
| ] | |
| prompt = ChatPromptTemplate.from_messages(messages) | |
| chain = prompt | ChatGroq(model_name="qwen/qwen3.8-27b") | StrOutputParser() | |
| image_summaries = [] | |
| print(f"⏳ Analyzing {len(images)} images sequentially...") | |
| for img in images: | |
| try: | |
| # Use invoke() instead of batch() for better control | |
| summary = chain.invoke({"image": img}) | |
| image_summaries.append(summary) | |
| time.sleep(3.0) # Vision models need more time to avoid rate limits | |
| except Exception as e: | |
| print(f"⚠️ Image skip: {e}") | |
| image_summaries.append("Image analysis failed.") | |
| return image_summaries | |
| def run_summarization_pipeline(tables, text_chunks, images): | |
| text_sums, table_sums = create_text_and_table_summaries(text_chunks, tables) | |
| image_sums = create_image_summaries(images) | |
| return text_sums, table_sums, image_sums | |