Spaces:
Build error
Build error
Update app.py
Browse files13 unmodified lines141516171819202187 unmodified lines1091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421436 unmodified lines15015115215315415515615715815913 unmodified linesnltk.download("punkt")nltk.download("punkt_tab")# Get the Gemini API key from Hugging Face secretsgemini_api_key = os.getenv("GEMINI_API_KEY")# Load a pre-trained embedding modelembedding_model = SentenceTransformer('all-MiniLM-L6-v2')87 unmodified linesprint(f"API request failed: {e}")return None# Function to generate follow-up questions based on the responsedef generate_follow_up_questions(user_input, bot_response):url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={gemini_api_key}"headers = {"Content-Type": "application/json",}follow_up_data = {"contents": [{"parts": [{"text": f"Based on the following user query and bot response, generate three follow-up questions to continue the conversation. Query: '{user_input}', Bot Response: '{bot_response}'"}]}]}try:response = requests.post(url, json=follow_up_data, headers=headers)response.raise_for_status() # Raise an error for bad status codesresponse_data = response.json()if "candidates" in response_data and len(response_data["candidates"]) > 0:follow_up_questions = response_data["candidates"][0]["content"]["parts"][0]["text"].strip()follow_up_questions = follow_up_questions.split("\n") # Split questions by newlinereturn follow_up_questionselse:return ["I couldn't generate follow-up questions."]except requests.exceptions.RequestException as e:print(f"API request failed: {e}")return ["Error generating follow-up questions."]# Function to generate bot response with context and semantic searchdef generate_bot_response(user_input):relevant_text = ""6 unmodified lines# Query Gemini API with the relevant contextgemini_response = query_gemini_api(relevant_text, user_input)if gemini_response:# Generate follow-up questionsfollow_up_questions = generate_follow_up_questions(user_input, gemini_response)# Append the follow-up questions to the responsegemini_response += "\n\n**Follow-up Questions:**\n" + "\n".join([f"{i+1}. {q}" for i, q in enumerate(follow_up_questions)])return gemini_responseelse:# Fallback to the original text if Gemini API fails13 unmodified lines141516171819202122232487 unmodified lines1121131141151161176 unmodified lines12412512612712812913 unmodified linesnltk.download("punkt")nltk.download("punkt_tab")# Replace with your Gemini API key stored in Hugging Face Secretsgemini_api_key = os.getenv("GEMINI_API_KEY") # Access the API key securely from Hugging Face Secretsif not gemini_api_key:raise ValueError("Gemini API key is missing. Please ensure it's set in Hugging Face Secrets.")# Load a pre-trained embedding modelembedding_model = SentenceTransformer('all-MiniLM-L6-v2')87 unmodified linesprint(f"API request failed: {e}")return None# Function to generate bot response with context and semantic searchdef generate_bot_response(user_input):relevant_text = ""6 unmodified lines# Query Gemini API with the relevant contextgemini_response = query_gemini_api(relevant_text, user_input)if gemini_response:return gemini_responseelse:# Fallback to the original text if Gemini API fails
app.py+5-35