| 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 |
|
|
| |
| os.environ["GOOGLE_API_KEY"] = os.environ.get("GOOGLE_API_KEY") |
|
|
| |
| model = ChatGoogleGenerativeAI( |
| model="models/gemini-1.5-flash-latest", |
| temperature=0.5 |
| ) |
|
|
| |
| template = PromptTemplate( |
| template="Generate 5 interesting facts about {topic}", |
| input_variables=["topic"] |
| ) |
|
|
| parser = StrOutputParser() |
| chain = template | model | parser |
|
|
| |
| 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() |
|
|