Spaces:
Build error
Build error
Update app.py
Browse files12345675 unmodified lines13141516171819202122232425262728293031323334353637import gradio as grimport google.generativeai as genaiimport osfrom process_docs import load_documents # Import our document processing script# Load API Key from Hugging Face SecretsGEMINI_API_KEY = os.getenv("GEMINI_API_KEY")5 unmodified lines# Load document text into memorydocuments_text = load_documents()def chatbot_response(user_input, history=[]):"""Handles user queries and generates responses using Gemini API.It considers chat history and document context."""# Combine user input with document contextprompt = f"Use the following document data to answer:\n{documents_text}\n\nUser: {user_input}"# Call Gemini APIresponse = model.generate_content(prompt)# Extract response textanswer = response.text if hasattr(response, "text") else "Sorry, I couldn't generate a response."return answer # Send response back to Gradio UI# Create a Gradio chat interfaceiface = gr.ChatInterface(fn=chatbot_response, title="Document-Aware Chatbot", chatbot=gr.Chatbot(type="messages"))# Launch the chatbot appiface.launch()12345675 unmodified lines1314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import gradio as grimport google.generativeai as genaiimport osfrom process_docs import load_documents # Import document processing script# Load API Key from Hugging Face SecretsGEMINI_API_KEY = os.getenv("GEMINI_API_KEY")5 unmodified lines# Load document text into memorydocuments_text = load_documents()def chatbot_response(user_input, history):"""Handles user queries, maintains conversation memory, and uses the new Gradio messages format."""if history is None:history = [] # Initialize empty history if None# Convert history to the new formatformatted_history = "\n".join([f"{h['role']}: {h['content']}" for h in history[-5:]]) # Keep last 5 messages# Construct prompt with memory + document contextprompt = f"""Use the following conversation history and document data to answer the user's question.Conversation History:{formatted_history}Document Data:{documents_text}User: {user_input}Bot:"""# Call Gemini APIresponse = model.generate_content(prompt)# Extract response textanswer = response.text if hasattr(response, "text") else "Sorry, I couldn't generate a response."# Append new message to history in the correct formathistory.append({"role": "user", "content": user_input})history.append({"role": "assistant", "content": answer})return answer, history # Return both response and updated history# Create a Gradio chat interface with memory using new "messages" formatiface = gr.ChatInterface(fn=chatbot_response, title="Memory-Enabled Chatbot", chatbot=gr.Chatbot(type="messages"))# Launch the chatbot appiface.launch()
app.py+30-11