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