Asyagul1993's picture
Update app.py
3dc4373 verified
Raw History Blame Contribute Delete
3.5 kB
!pip install -q gradio openai
import os
import gradio as gr
from openai import OpenAI
# πŸ”‘ SET YOUR GROQ API KEY HERE
os.environ["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"] = "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"
# πŸ€– Create Groq client
client =OpenAI(
base_url="https://api.groq.com/openai/v1",
api_key=os.environ.get("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")
)
# 🧬 System prompt
SYSTEM_PROMPT = """
You are a Bioinformatics AI assistant.
You help with:
- DNA, RNA, Protein analysis
- FASTA format
- BLAST explanation
- Genetics and mutations
Give simple, clear, scientific answers.
"""
# 🧠 memory storage
history = []
# 🧬 GC Content function
def gc_content(seq):
seq = seq.upper()
return round((seq.count("G")+seq.count("C"))/len(seq)*100, 2) if len(seq)>0 else 0
# πŸ’¬ Chat function
def chat(message):
global history
# 🧬 Auto DNA detection
seq = message.replace(" ", "").upper()
if len(seq) > 10 and all(c in "ATGC" for c in seq):
reply = f"""🧬 DNA Analysis:
Length: {len(seq)}
GC Content: {gc_content(seq)}%
Sequence looks valid DNA.
"""
history.append((message, reply))
return history
# πŸ€– Build messages
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
for u, b in history:
messages.append({"role": "user", "content": u})
messages.append({"role": "assistant", "content": b})
messages.append({"role": "user", "content": message})
# πŸ”₯ API call
response = client.chat.completions.create(
model="openai/gpt-oss-20b",
messages=messages,
temperature=0.7
)
reply = response.choices[0].message.content
history.append((message, reply))
return history
# 🎨 Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# 🧬 Bioinformatics AI")
chatbot = gr.Chatbot()
msg = gr.Textbox(placeholder="Ask DNA, RNA, FASTA, BLAST or paste sequence...")
btn = gr.Button("Send")
clear = gr.Button("Clear Chat")
def respond(m):
return chat(m)
msg.submit(respond, msg, chatbot)
btn.click(respond, msg, chatbot)
clear.click(lambda: [], None, chatbot)
demo.launch()
# πŸ”Ή Install required libraries
!pip install -q openai gradio
# πŸ”Ή Imports
import os
import gradio as gr
from openai import OpenAI
# πŸ”Ή Set your Groq API Key
os.environ["GROQ_API_KEY"] = "YOUR_GROQ_API_KEY_HERE"
# πŸ”Ή Groq client (OpenAI compatible)
client = OpenAI(
base_url="https://api.groq.com/openai/v1",
api_key=os.environ["GROQ_API_KEY"]
)
# πŸ”¬ Bioinformatics system prompt
SYSTEM_PROMPT = """
You are a professional Bioinformatics AI assistant.
You help with DNA, RNA, protein analysis, BLAST, FASTA, gene functions, mutations, and sequencing.
Explain clearly like ChatGPT in simple + expert format.
"""
# πŸ”Ή Chat function
def chat(user_input, history):
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
for h in history:
messages.append({"role": "user", "content": h[0]})
messages.append({"role": "assistant", "content": h[1]})
messages.append({"role": "user", "content": user_input})
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=messages
)
return response.choices[0].message.content
# πŸ”Ή Gradio UI
chatbot = gr.ChatInterface(fn=chat, title="🧬 Bioinformatics AI Chatbot (Groq)")
chatbot.launch()