Chat with Gemini AI from Your Terminal using Python ✨🤖🚀

Hey it's boy SaadMaqsood 🧑‍💻 back again on LearnTermux.tech, where we don’t do boring. Today I’ve got something hella cool — we’re turning our terminal into a literal chatbox with Google Gemini AI 💬✨ Yes, you heard that right. Chatting with AI like it’s your buddy, and all of that right inside your Termux/Python setup. No fluff, just vibes and code.

Let’s goo!!




What’s cooking?

We’re building a Python script that lets you talk to Gemini 1.5 Flash (the fast, lightweight version of Gemini) like you’re texting your AI bestie.

No extra libraries. No complicated junk. Just clean requests, JSON, and your ✨mind✨.


Here's the exact code you need to copy-paste:

import requests
import json

# 🔐 Your Gemini API Key 👇🏻👇🏻👇🏻 (Generate from Google's AI Studio)
API_KEY = "PASTE_YOUR_API_KEY_HERE"

# 🌐 Gemini Endpoint
URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={API_KEY}"

# 🧠 Headers
HEADERS = {
    "Content-Type": "application/json"
}

print("🔹 Chat with Gemini (Type 'exit' to stop) 🔹\n")

while True:
    user_input = input("You: ")  # 🎤 Taking user input

    if user_input.lower() in ["exit", "quit"]:  # ✌️ Exit condition
        print("👋 Exiting chat...")
        break

    data = {
        "contents": [{
            "parts": [{"text": user_input}]
        }]
    }

    # 🚀 Sending the request
    response = requests.post(URL, headers=HEADERS, data=json.dumps(data))

    # ✅ Handling response
    if response.status_code == 200:
        result = response.json()
        try:
            bot_response = result["candidates"][0]["content"]["parts"][0]["text"]
            print(f"Gemini: {bot_response}\n")
        except (KeyError, IndexError):
            print("❌ Error: Unexpected response format.\n")
    else:
        print(f"❌ API Error: {response.status_code} - {response.text}\n")


How to get your Gemini API Key 🔑

  1. Go to → Google AI Studio
  2. Login with your Gmail.
  3. Generate your API key from the ⚙️ Settings tab.
  4. Copy it and paste it inside the API_KEY = "..." line.


How to run the chat:

  1. Save the code in a .py file. Like geminiChat.py
  2. Open Termux or any terminal with Python installed.
  3. Run:
python geminiChat.py
  1. Start chatting with Gemini like it’s ChatGPT’s twin brother ✨




Pro Tip:

This works fire with termux-api and even your Android keyboard. Imagine creating bots or full automation using AI — all from your phone. The possibilities? Endless.


Final Words:

If you're a nerdy chaotic soul like me, this script is peak productivity. Talk to your AI, get ideas, generate code, or just vent to it when your scripts are broken and your life is in shambles.

Gemini gotchu. I gotchu. You got this.

And yeahhh, that’s it for now. If this post helped you even a little, make sure to hit that TikTok follow on @learntermux.tech for daily spicy content, or just vibe with me in the Discord.

Peace out hackers & creatives! Stay ethical! ✌️👾

Post a Comment

0 Comments

Popup Image