Kunal commited on
Commit
415f853
·
1 Parent(s): c2b5185

fixed base model and litellm configration

Browse files
Files changed (2) hide show
  1. app.py +66 -40
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,16 +1,16 @@
1
- import gradio as gr
2
- from smolagents import LiteLLMModel, CodeAgent, tool
3
  import os
 
 
4
  from pypdf import PdfReader
5
- from litellm import completion
6
 
7
- #Initialize the Model
8
- model = LiteLLMModel(
9
- model_name="gemini/gemini-1.5-flash",
10
- api_key=os.environ.get("GEMINI_API_KEY")
11
- )
12
 
13
- #tool to extract text from PDF
14
  @tool
15
  def process_pdf(file_path: str) -> str:
16
  """
@@ -20,55 +20,81 @@ def process_pdf(file_path: str) -> str:
20
  Returns:
21
  str: Extracted text from the PDF.
22
  """
23
- reader = PdfReader(file_path)
24
- return "\n".join([page.extract_text() for page in reader.pages])
 
 
 
25
 
26
- #tool to analysis the pdf for answering question
27
  @tool
28
- def chat_with_pdf(query: str, pdf_text: str, chat_history: list) -> str:
29
  """
30
  Answer questions about the PDF content.
31
  Args:
32
- query (str): The question to answer.
33
- pdf_text (str): The text extracted from the PDF.
34
- chat_history (list): Previous chat history.
35
  Returns:
36
- str: Answer to the question.
37
  """
 
38
  pass
39
 
40
- #Agent to handle the chat with PDF
 
 
 
 
 
 
41
  agent = CodeAgent(
42
- model=model,
43
- model_kwargs={
44
- "model": "gemini/gemini-pro",
45
- "api_base": "https://generativelanguage.googleapis.com/v1beta", # Correct Google API endpoint
46
- "api_key": os.getenv("GEMINI_API_KEY") # Ensure key is set in environment
47
- },
48
- tools=[process_pdf, chat_with_pdf]
49
  )
50
- #Gradio Interface
 
51
  def process_pdf_ui(file):
52
- pdf_text = process_pdf(file.name)
53
- return pdf_text
 
54
 
55
- def chat_ui(query, history, pdf_text):
56
- response = agent.run(
57
- f"PDF Context: {pdf_text}\nUser Query: {query}\nChat History: {history}"
58
- )
59
- return response, history + [(query, response)]
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  with gr.Blocks() as demo:
62
- gr.Markdown("# PDF Chatbot")
63
  with gr.Row():
64
- pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
65
- pdf_text = gr.Textbox(visible=False)
 
 
 
 
66
 
67
- chatbot = gr.Chatbot(type="messages")
68
- msg = gr.Textbox(placeholder="Ask a question about the PDF")
 
 
 
69
 
70
- pdf_input.upload(process_pdf_ui, inputs=pdf_input, outputs=pdf_text)
71
- msg.submit(chat_ui, inputs=[msg, chatbot, pdf_text], outputs=[chatbot, msg])
 
 
 
72
 
73
  if __name__ == "__main__":
74
  demo.launch()
 
 
 
1
  import os
2
+ import gradio as gr
3
+ from smolagents import CodeAgent, tool, LiteLLMModel
4
  from pypdf import PdfReader
5
+ from dotenv import load_dotenv
6
 
7
+ # Load environment variables (for local dev)
8
+ load_dotenv()
9
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
10
+ if not GEMINI_API_KEY:
11
+ raise ValueError("GEMINI_API_KEY not set. Please set it in your .env or Hugging Face Space secrets.")
12
 
13
+ # --------- TOOLS ---------
14
  @tool
15
  def process_pdf(file_path: str) -> str:
16
  """
 
20
  Returns:
21
  str: Extracted text from the PDF.
22
  """
23
+ try:
24
+ reader = PdfReader(file_path)
25
+ return "\n".join([page.extract_text() or "" for page in reader.pages])
26
+ except Exception as e:
27
+ return f"PDF extraction failed: {str(e)}"
28
 
 
29
  @tool
30
+ def chat_with_pdf(query: str, pdf_text: str) -> str:
31
  """
32
  Answer questions about the PDF content.
33
  Args:
34
+ query (str): The user's question.
35
+ pdf_text (str): Text extracted from the PDF.
 
36
  Returns:
37
+ str: The answer to the question based on the PDF content.
38
  """
39
+ # The actual logic is handled by the agent's LLM
40
  pass
41
 
42
+ # --------- AGENT SETUP ---------
43
+ model = LiteLLMModel(
44
+ model_id="gemini/gemini-1.5-flash", # Use "gemini/gemini-1.5-pro" if you have access
45
+ api_key=GEMINI_API_KEY
46
+ # Do NOT set api_base for Gemini AI Studio keys!
47
+ )
48
+
49
  agent = CodeAgent(
50
+ tools=[process_pdf, chat_with_pdf],
51
+ model=model
 
 
 
 
 
52
  )
53
+
54
+ # --------- GRADIO INTERFACE ---------
55
  def process_pdf_ui(file):
56
+ if not file:
57
+ return ""
58
+ return process_pdf(file.name)
59
 
60
+ def chat_ui(message, history, pdf_text):
61
+ if not pdf_text:
62
+ return [{"role": "assistant", "content": "Please upload a PDF first."}]
63
+ # Compose a prompt for the agent
64
+ prompt = f"PDF Content:\n{pdf_text}\n\nUser Question: {message}"
65
+ try:
66
+ response = agent.run(prompt)
67
+ # Return response in OpenAI-style message format for Gradio
68
+ history = history or []
69
+ history.append({"role": "user", "content": message})
70
+ history.append({"role": "assistant", "content": response})
71
+ return history
72
+ except Exception as e:
73
+ history = history or []
74
+ history.append({"role": "assistant", "content": f"Error: {str(e)}"})
75
+ return history
76
 
77
  with gr.Blocks() as demo:
78
+ gr.Markdown("# 📄 Chat with your PDF (Gemini AI)")
79
  with gr.Row():
80
+ with gr.Column(scale=1):
81
+ pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
82
+ pdf_text = gr.Textbox(visible=False)
83
+ with gr.Column(scale=3):
84
+ chatbot = gr.Chatbot(label="PDF Chat", height=400, type="messages")
85
+ msg = gr.Textbox(label="Ask a question about the PDF", placeholder="Type your question and hit Enter...")
86
 
87
+ pdf_input.upload(
88
+ fn=process_pdf_ui,
89
+ inputs=pdf_input,
90
+ outputs=pdf_text
91
+ )
92
 
93
+ msg.submit(
94
+ fn=chat_ui,
95
+ inputs=[msg, chatbot, pdf_text],
96
+ outputs=chatbot
97
+ )
98
 
99
  if __name__ == "__main__":
100
  demo.launch()
requirements.txt CHANGED
@@ -4,3 +4,4 @@ smolagents[litellm]
4
  pypdf
5
  gradio
6
  litellm
 
 
4
  pypdf
5
  gradio
6
  litellm
7
+ python-dotenv