Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
+
|
| 5 |
+
llm = None # Global Gemini model
|
| 6 |
+
|
| 7 |
+
# Set up Gemini model
|
| 8 |
+
def setup_gemini(api_key):
|
| 9 |
+
try:
|
| 10 |
+
os.environ["GOOGLE_API_KEY"] = api_key
|
| 11 |
+
global llm
|
| 12 |
+
llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.2)
|
| 13 |
+
return "✅ Gemini model is ready!"
|
| 14 |
+
except Exception as e:
|
| 15 |
+
return f"❌ Error: {str(e)}"
|
| 16 |
+
|
| 17 |
+
# Ask question using Chain of Thought prompting
|
| 18 |
+
def solve_step_by_step(question):
|
| 19 |
+
if not llm:
|
| 20 |
+
return "⚠️ Please initialize Gemini first."
|
| 21 |
+
|
| 22 |
+
prompt = question.strip()
|
| 23 |
+
if not prompt.endswith("Let's think step by step."):
|
| 24 |
+
prompt += " Let's think step by step."
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
return llm.invoke(prompt)
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"❌ Error: {str(e)}"
|
| 30 |
+
|
| 31 |
+
# Gradio UI
|
| 32 |
+
with gr.Blocks(title="Chain of Thought with Gemini") as demo:
|
| 33 |
+
gr.Markdown("## 🤖 Chain of Thought Prompting (Gemini)")
|
| 34 |
+
|
| 35 |
+
api_key = gr.Textbox(label="🔑 Gemini API Key", type="password", placeholder="Paste your Gemini key")
|
| 36 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 37 |
+
|
| 38 |
+
setup_btn = gr.Button("⚙️ Initialize Gemini")
|
| 39 |
+
|
| 40 |
+
question = gr.Textbox(label="❓ Your Question", placeholder="e.g., What is 15 * 4?")
|
| 41 |
+
answer = gr.Textbox(label="🧠 Gemini's Reasoning", lines=10)
|
| 42 |
+
|
| 43 |
+
ask_btn = gr.Button("🔍 Think Step by Step")
|
| 44 |
+
|
| 45 |
+
setup_btn.click(fn=setup_gemini, inputs=[api_key], outputs=[status])
|
| 46 |
+
ask_btn.click(fn=solve_step_by_step, inputs=[question], outputs=[answer])
|
| 47 |
+
|
| 48 |
+
demo.launch()
|