CoolFace
Apppublic

abbukhadka/AI_Powered_Conversational_App

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py42 linesDownload Raw Back to root
1import streamlit as st2from langchain_google_genai import ChatGoogleGenerativeAI3from langchain_core.messages import HumanMessage  # Required for chat-style input4 5# Function to return the response6def load_answer(question):7    if not question or question.strip() == "":8        raise ValueError("Question is empty. Please provide valid input.")9 10    llm = ChatGoogleGenerativeAI(11        model="gemini-2.0-flash",       # Gemini model12        temperature=0.7,                # Adjustable creativity13        max_tokens=None,14        timeout=None,15        max_retries=216    )17    18    # Pass the user question as a HumanMessage in a list19    message = HumanMessage(content=question)20    answer = llm.invoke([message])  # Must be a list of messages21    return answer.content22 23# Streamlit app setup24st.set_page_config(page_title="LangChain Gemini Chat", page_icon="๐Ÿค–")25st.header("๐ŸŽฌ LangChain + Gemini Assistant")26 27# Input from user28user_input = st.text_input("You:", key="input")29 30# Button to trigger LLM31if st.button("Generate"):32    if not user_input.strip():33        st.warning("Please enter a prompt before generating.")34    else:35        with st.spinner("Thinking..."):36            try:37                response = load_answer(user_input)38                st.subheader("Answer:")39                st.write(response)40            except Exception as e:41                st.error(f"โŒ Error: {str(e)}")42