File size: 1,087 Bytes
5e61b54 161e734 4825ce7 161e734 4825ce7 5e61b54 4825ce7 23b6243 5e61b54 4825ce7 5e61b54 4825ce7 5e61b54 4825ce7 161e734 4825ce7 5e61b54 4825ce7 | 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 | 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()
|