Chain / app.py
koler's picture
Update app.py
4825ce7 verified
Raw
History Blame Contribute Delete
1.09 kB
import os
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
import gradio as gr
# loading api key
os.environ["GOOGLE_API_KEY"] = os.environ.get("GOOGLE_API_KEY")
# loading model, here i am using Google model, because huggingFace model is not working for me, due to api issue.
model = ChatGoogleGenerativeAI(
model="models/gemini-1.5-flash-latest",
temperature=0.5
)
# Creating a simple template
template = PromptTemplate(
template="Generate 5 interesting facts about {topic}",
input_variables=["topic"]
)
parser = StrOutputParser()
chain = template | model | parser
# gradio code
def generate_facts(topic):
try:
return chain.invoke({'topic': topic})
except Exception as e:
return f"Error: {e}"
iface = gr.Interface(
fn=generate_facts,
inputs=gr.Textbox(label="Enter Topic"),
outputs=gr.Textbox(label="5 Interesting Facts"),
title="Fact Generator using Gemini"
)
if __name__ == "__main__":
iface.launch()