Spaces:
Build error
Build error
File size: 2,948 Bytes
7d5a9bb d8faf9e 355ce95 d8faf9e 8479253 0f85a54 7d5a9bb 355ce95 0d40f0f 0f85a54 355ce95 8479253 355ce95 44b87db 355ce95 44b87db 0f85a54 355ce95 8479253 0f85a54 8479253 355ce95 f5b7986 0d40f0f 355ce95 f5b7986 0d40f0f 355ce95 0d40f0f 355ce95 d8faf9e 0f85a54 8479253 355ce95 8479253 355ce95 8479253 7d5a9bb 0f85a54 44b87db 8479253 | 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 | import os
import docx
import requests
import gradio as gr
from io import BytesIO
# β
API Key from Hugging Face Secrets
API_KEY = os.getenv("GEMINI_API_KEY") # Set this in Hugging Face secrets!
# β
Function to Download File from Link
def download_file_from_link(file_link):
"""Download a file from the provided link."""
try:
response = requests.get(file_link)
response.raise_for_status() # Raise an error for bad status codes
return BytesIO(response.content) # Return file content as a BytesIO object
except Exception as e:
return f"β Error downloading file: {e}"
# β
Function to Load and Read File Contents
def load_data(file_link):
"""Read content from the downloaded file."""
file_content = download_file_from_link(file_link)
if isinstance(file_content, str): # If an error message is returned
return file_content
try:
doc = docx.Document(file_content)
return "\n".join([para.text for para in doc.paragraphs if para.text.strip()])
except Exception as e:
return f"β Error reading file: {e}"
# β
Function to Call Google AI Gemini API
def call_gemini_api(file_link, user_input):
"""Send user query + document content to Google AI."""
if not API_KEY:
return "β Error: API Key not found! Add it as GEMINI_API_KEY in Hugging Face secrets."
# Read document content
document_content = load_data(file_link)
if document_content.startswith("β"):
return document_content # Return error message if file loading failed
# Truncate document content to avoid exceeding API limits
max_context_length = 4000 # Adjust based on API limits
truncated_content = document_content[:max_context_length]
# Construct API request
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={API_KEY}"
headers = {"Content-Type": "application/json"}
payload = {
"contents": [
{
"parts": [
{"text": f"Document Context: {truncated_content}\nUser Query: {user_input}"}
]
}
]
}
# Make API call
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status() # Raise an error for bad status codes
return response.json().get("candidates", [{}])[0].get("content", "No response from API")
except Exception as e:
return f"β API Error: {e}"
# β
Gradio Interface
iface = gr.Interface(
fn=call_gemini_api,
inputs=[
gr.Textbox(label="File Link", placeholder="Paste the file link here (e.g., Google Drive link)"),
gr.Textbox(label="Ask a question")
],
outputs="text",
title="π AI Chatbot with Real-Time Document Context",
description="This chatbot answers questions based on the document fetched from the provided link."
)
iface.launch()
|