Spaces:
Sleeping
Sleeping
import os | |
from langchain_core.prompts import PromptTemplate | |
from langchain_community.output_parsers.rail_parser import GuardrailsOutputParser | |
from langchain_community.document_loaders import PyPDFLoader | |
import google.generativeai as genai | |
import gradio as gr | |
# Function for initialization | |
def initialize(pdf_file, question): | |
try: | |
# Access the uploaded file information from Gradio | |
file_info = pdf_file | |
# Check if a file was uploaded | |
if file_info is not None: | |
# Construct potential file path based on temporary directory and filename | |
file_path = os.path.join("/tmp", file_info.name) # Adjust temporary directory if needed | |
if os.path.exists(file_path): | |
# Process the PDF | |
pdf_loader = PyPDFLoader(file_path) | |
pages = pdf_loader.load_and_split() | |
processed_context = "\n".join(str(page.page_content) for page in pages[:30]) # Limit to first 30 pages | |
# Configure Google Generative AI (replace with your API key) | |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) | |
# Prompt template for formatting context and question | |
prompt_template = """Answer the question as precise as possible using the provided context. If the answer is not contained in the context, say "answer not available in context" | |
Context: | |
{context} | |
Question: | |
{question} | |
Answer: | |
""" | |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"]) | |
# Load the GeminiPro model | |
model = genai.GenerativeModel('gemini-pro') | |
# Option 1: Using GeminiPro's Text Generation (if applicable) | |
# Check if the model has a 'generate' method (or similar) - adjust based on actual method | |
if hasattr(model, 'generate'): | |
# Process context and question (already done) | |
# Generate answer using GeminiPro's generate method | |
generated_answer = model.generate(prompt=prompt) # Replace with the appropriate method | |
# Extract the answer (parse the output from 'generate') | |
# ... (implementation depends on the model's output format) | |
return generated_answer | |
# Option 2: Alternative LLM Integration (if GeminiPro methods not suitable) | |
# Replace this section with code using an alternative library/framework | |
# for question answering (e.g., transformers, haystack) | |
# Ensure the code integrates with your chosen LLM and handles context processing, | |
# question answering, and answer extraction. | |
# Example placeholder (replace with your actual implementation): | |
# return "Alternative LLM integration not yet implemented." | |
else: | |
return "Error: The uploaded file could not be found." | |
else: | |
return "Error: No PDF file was uploaded." | |
except Exception as e: | |
return f"An error occurred: {e}" # Generic error handling | |
# Create a Gradio interface | |
interface = gr.Interface( | |
fn=initialize, | |
inputs=[ | |
gr.File(label="Upload PDF"), # No need for 'type' argument | |
gr.Textbox(label="Question") | |
], | |
outputs="text", | |
title="GeminiPro Q&A Bot", | |
description="Ask questions about the uploaded PDF document.", | |
) | |
# Launch the interface | |
interface.launch() | |