Download app.py from koler/Chain: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/koler/Chain/resolve/main/app.py
- Command line
-
hf download hf://spaces/koler/Chain/app.py
-
curl -L -o app.py https://huggingface.co/spaces/koler/Chain/resolve/main/app.py
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() | |