Spaces:
Running
Running
File size: 7,158 Bytes
26783c4 8fc9e84 a226bc4 26783c4 8fc9e84 5a69f4d 21931b3 8fc9e84 5a69f4d 0a358e5 5a69f4d 8fc9e84 5a69f4d 8fc9e84 5a69f4d 21931b3 8fc9e84 5a69f4d 8fc9e84 5a69f4d 8fc9e84 0a358e5 30b24b6 8fc9e84 9588578 8fc9e84 5a69f4d |
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 |
import gradio as gr
from markitdown import MarkItDown
import google.generativeai as genai
import tempfile
import os
from pathlib import Path
# Initialize MarkItDown
md = MarkItDown()
# Configure Gemini AI
genai.configure(api_key=os.getenv('GEMINI_KEY'))
model = genai.GenerativeModel('gemini-2.0-flash-exp')
def process_with_markitdown(input_path):
"""Process file or URL with MarkItDown and return text content"""
try:
result = md.convert(input_path)
return result.text_content
except Exception as e:
return f"Error processing input: {str(e)}"
def save_uploaded_file(file):
"""Save uploaded file to temporary location and return path"""
if file is None:
return None
try:
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, file.name)
with open(temp_path, 'wb') as f:
f.write(file.read())
return temp_path
except Exception as e:
return f"Error saving file: {str(e)}"
async def summarize_text(text):
"""Summarize the input text using Gemini AI"""
try:
prompt = f"""Please provide a concise summary of the following text. Focus on the main points and key takeaways:
{text}
Summary:"""
response = await model.generate_content_async(prompt)
return response.text
except Exception as e:
return f"Error generating summary: {str(e)}"
async def process_input(input_text, uploaded_file=None):
"""Main function to process either URL or uploaded file"""
try:
if uploaded_file is not None:
# Handle file upload
temp_path = save_uploaded_file(uploaded_file)
if temp_path.startswith('Error'):
return temp_path
text = process_with_markitdown(temp_path)
# Clean up temporary file
try:
os.remove(temp_path)
except:
pass
elif input_text.startswith(('http://', 'https://')):
# Handle URL
text = process_with_markitdown(input_text)
else:
# Handle direct text input
text = input_text
if text.startswith('Error'):
return text
# Generate summary using Gemini AI
summary = await summarize_text(text)
return summary
except Exception as e:
return f"Error processing input: {str(e)}"
def clear_inputs():
return ["", None, ""]
# Create Gradio interface with drag-and-drop
with gr.Blocks(theme=gr.themes.Soft()) as iface:
gr.Markdown(
"""
# Summarizeit
> Summarize any document!
Enter a URL, paste text, or drag & drop a file to get a summary.
"""
)
with gr.Row():
input_text = gr.Textbox(
label="Enter URL or text",
placeholder="Enter a URL or paste text here...",
scale=2
)
with gr.Row():
file_upload = gr.File(
label="Drop files here or click to upload",
file_types=[
".pdf", ".docx", ".xlsx", ".csv", ".txt", ".md",
".html", ".htm", ".xml", ".json"
],
file_count="single",
scale=2
)
with gr.Row():
submit_btn = gr.Button("Summarize", variant="primary")
clear_btn = gr.Button("Clear")
output_text = gr.Textbox(
label="Summary",
lines=10,
show_copy_button=True
)
# Set up event handlers
submit_btn.click(
fn=process_input,
inputs=[input_text, file_upload],
outputs=output_text
)
clear_btn.click(
fn=clear_inputs,
outputs=[input_text, file_upload, output_text]
)
# Add examples
gr.Examples(
examples=[
["https://h3manth.com"],
["https://www.youtube.com/watch?v=bSHp7WVpPgc"],
["https://en.wikipedia.org/wiki/Three-body_problem"],
["""
The discovery of the Higgs boson in 2012 was a major milestone in the history of particle physics. The Higgs boson is a fundamental particle that is responsible for giving mass to other particles. It is named after physicist Peter Higgs, who along with others, proposed its existence in the 1960s.
The Higgs boson is a scalar boson, which means it has zero spin. It is also a very heavy particle, with a mass of approximately 125 GeV (gigaelectronvolts). This is much heavier than other fundamental particles, such as electrons and quarks, which have masses measured in units of MeV (million electronvolts).
The Higgs boson is produced in high-energy collisions between particles, such as those produced by the Large Hadron Collider (LHC) at CERN. The LHC is a powerful particle accelerator that smashes protons together at incredibly high energies, creating a shower of particles that can be detected by sophisticated detectors.
The detection of the Higgs boson was a major challenge for physicists, as it is a very rare event. The LHC produces an enormous number of particles in each collision, but the Higgs boson is only produced in a tiny fraction of these collisions. To detect the Higgs boson, physicists had to develop sophisticated algorithms and analysis techniques to sift through the vast amounts of data produced by the LHC.
The discovery of the Higgs boson was announced in 2012 by the ATLAS and CMS experiments at the LHC. The discovery was confirmed by subsequent experiments and was recognized as one of the most significant scientific discoveries of the 21st century.
The Higgs boson has many important implications for our understanding of the universe. It provides evidence for the existence of the Higgs field, a fundamental field that permeates all of space and gives mass to particles. The Higgs boson also provides a window into the early universe, allowing physicists to study the conditions that existed in the universe just after the Big Bang.
In addition to its scientific significance, the discovery of the Higgs boson has also had a significant impact on society. It has inspired a new generation of scientists and engineers, and has led to the development of new technologies and applications.
The search for new physics beyond the Standard Model of particle physics is an active area of research. The Higgs boson is a key part of the Standard Model, but it is not the only particle that is predicted by the theory. There are many other particles that are predicted by the Standard Model, but have not yet been detected.
The search for new physics beyond the Standard Model is an exciting and challenging area of research. It requires the development of new detectors and analysis techniques, as well as the construction of new particle accelerators. The discovery of new particles and forces could have a major impact on our understanding of the universe and could lead to the development of new technologies and applications.
"""]
],
inputs=input_text
)
if __name__ == "__main__":
iface.launch() |