agharsallah commited on
Commit
f928c5e
·
1 Parent(s): f86da75

addding simple llama parse tool

Browse files
Files changed (4) hide show
  1. .env.example +1 -0
  2. app.py +31 -17
  3. requirements.txt +1 -0
  4. tools.py +51 -0
.env.example ADDED
@@ -0,0 +1 @@
 
 
1
+ LLAMA_CLOUD_API_KEY= your_api_key_here
app.py CHANGED
@@ -1,26 +1,40 @@
1
  import gradio as gr
 
 
2
 
 
 
 
 
 
 
 
 
3
 
4
- def letter_counter(word, letter):
5
- """
6
- Count the occurrences of a specific letter in a word.
7
 
8
- Args:
9
- word: The word or phrase to analyze
10
- letter: The letter to count occurrences of
 
 
 
 
11
 
12
- Returns:
13
- The number of times the letter appears in the word
14
- """
15
- return word.lower().count(letter.lower())
16
 
 
 
 
 
 
 
 
 
 
17
 
18
- demo = gr.Interface(
19
- fn=letter_counter,
20
- inputs=["text", "text"],
21
- outputs="number",
22
- title="Letter Counter",
23
- description="Count how many times a letter appears in a word",
24
  )
25
 
26
- demo.launch(mcp_server=True)
 
 
1
  import gradio as gr
2
+ from tools import letter_counter, pdf_to_text
3
+ import os
4
 
5
+ # --- Gradio Tool: Letter Counter ---
6
+ letter_counter_tool = gr.Interface(
7
+ fn=letter_counter,
8
+ inputs=[gr.Textbox(label="Word or Phrase"), gr.Textbox(label="Letter")],
9
+ outputs=gr.Number(label="Count"),
10
+ title="Letter Counter",
11
+ description="Count how many times a letter appears in a word or phrase.",
12
+ )
13
 
 
 
 
14
 
15
+ # --- Gradio Tool: PDF to Text ---
16
+ def gradio_pdf_to_text(pdf_file, api_key=None):
17
+ if api_key is None:
18
+ api_key = os.getenv("LLAMA_CLOUD_API_KEY")
19
+ if pdf_file is None:
20
+ return "No PDF file provided."
21
+ return pdf_to_text(pdf_file.name, api_key=api_key)
22
 
 
 
 
 
23
 
24
+ pdf_to_text_tool = gr.Interface(
25
+ fn=gradio_pdf_to_text,
26
+ inputs=[
27
+ gr.File(label="PDF File"),
28
+ ],
29
+ outputs=gr.Textbox(label="Extracted Text"),
30
+ title="PDF to Text Extractor",
31
+ description="Extracts all text from a PDF using LlamaParse. Optionally provide your Llama Cloud API key.",
32
+ )
33
 
34
+ # --- Combine Tools in Gradio Tabbed UI ---
35
+ demo = gr.TabbedInterface(
36
+ [letter_counter_tool, pdf_to_text_tool], ["Letter Counter", "PDF to Text"]
 
 
 
37
  )
38
 
39
+ if __name__ == "__main__":
40
+ demo.launch(mcp_server=True)
requirements.txt CHANGED
@@ -1 +1,2 @@
1
  gradio[mcp]
 
 
1
  gradio[mcp]
2
+ llama-cloud-services
tools.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ tools.py
3
+
4
+ This module contains modular tools for the Gradio app, including:
5
+ - Letter Counter Tool
6
+ - PDF to Text Tool (using LlamaParse)
7
+ """
8
+
9
+ from llama_cloud_services import LlamaParse
10
+ import os
11
+
12
+
13
+ # --- Tool: Letter Counter ---
14
+ def letter_counter(word: str, letter: str) -> int:
15
+ """
16
+ Count the occurrences of a specific letter in a word.
17
+
18
+ Args:
19
+ word (str): The word or phrase to analyze.
20
+ letter (str): The letter to count occurrences of.
21
+
22
+ Returns:
23
+ int: The number of times the letter appears in the word.
24
+ """
25
+ return word.lower().count(letter.lower())
26
+
27
+
28
+ # --- Tool: PDF to Text ---
29
+ def pdf_to_text(pdf_path: str) -> str | None:
30
+ """
31
+ Extracts text from a PDF file using LlamaParse.
32
+
33
+ Args:
34
+ pdf_path (str): Path to the PDF file.
35
+
36
+ Returns:
37
+ str: The extracted text from the PDF.
38
+ None: If no text could be extracted.
39
+ """
40
+ parser = LlamaParse(
41
+ api_key=os.getenv("LLAMA_CLOUD_API_KEY"),
42
+ num_workers=1,
43
+ verbose=True,
44
+ language="en",
45
+ )
46
+ result = parser.parse(pdf_path)
47
+ # Get all text as a single string
48
+ text_documents = result.get_text_documents(split_by_page=False)
49
+ if text_documents:
50
+ return "\n".join([doc.text for doc in text_documents])
51
+ return None