Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,23 @@
|
|
| 1 |
-
# Combined
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
import logging
|
| 4 |
import re
|
| 5 |
-
import
|
| 6 |
-
import pickle
|
| 7 |
-
from typing import List, Tuple, Optional
|
| 8 |
import gradio as gr
|
| 9 |
from openai import OpenAI
|
| 10 |
import google.generativeai as genai
|
|
|
|
| 11 |
from functools import lru_cache
|
| 12 |
from tenacity import retry, stop_after_attempt, wait_exponential
|
| 13 |
from langchain_community.retrievers import BM25Retriever
|
| 14 |
from langchain_community.vectorstores import FAISS
|
| 15 |
from langchain_core.embeddings import Embeddings
|
| 16 |
from langchain_core.documents import Document
|
| 17 |
-
from collections import
|
| 18 |
-
defaultdict
|
| 19 |
import hashlib
|
| 20 |
from tqdm import tqdm
|
| 21 |
from dotenv import load_dotenv
|
|
@@ -205,30 +207,37 @@ class EnhancedRetriever:
|
|
| 205 |
context = []
|
| 206 |
for doc in docs:
|
| 207 |
context_str = f"""**Source**: [{doc.metadata['source']}]({doc.metadata['hyperlink']})
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
| 211 |
context.append(context_str)
|
| 212 |
return "\n\n---\n\n".join(context)
|
| 213 |
|
| 214 |
# --- Generation System ---
|
| 215 |
-
SYSTEM_PROMPT = """
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
-
Context: {context}
|
|
|
|
| 222 |
|
| 223 |
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=20))
|
| 224 |
def get_ai_response(query: str, context: str, model: str) -> str:
|
|
|
|
| 225 |
try:
|
| 226 |
if model == "gemini-2.0-flash":
|
| 227 |
gemini_model = genai.GenerativeModel(model)
|
| 228 |
response = gemini_model.generate_content(
|
| 229 |
f"{SYSTEM_PROMPT.format(context=context)}\nQuestion: {query}\nProvide a detailed technical answer:"
|
| 230 |
)
|
| 231 |
-
|
|
|
|
| 232 |
elif model == "meta-llama-3-70b-instruct":
|
| 233 |
response = client.chat.completions.create(
|
| 234 |
model=model,
|
|
@@ -239,7 +248,20 @@ def get_ai_response(query: str, context: str, model: str) -> str:
|
|
| 239 |
temperature=0.4,
|
| 240 |
max_tokens=2000
|
| 241 |
)
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
except Exception as e:
|
| 244 |
logger.error(f"Generation Error: {str(e)}")
|
| 245 |
return "I'm unable to generate a response right now. Please try again later."
|
|
@@ -249,6 +271,36 @@ def _postprocess_response(response: str) -> str:
|
|
| 249 |
response = re.sub(r"\*\*([\w-]+)\*\*", r"**\1**", response)
|
| 250 |
return response
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
# --- Pipeline ---
|
| 253 |
documents = load_and_chunk_data(data_file_name)
|
| 254 |
retriever = EnhancedRetriever(documents)
|
|
@@ -267,20 +319,20 @@ def chat_interface(question: str, history: List[Tuple[str, str]], model: str):
|
|
| 267 |
return "", history + [(question, response)]
|
| 268 |
|
| 269 |
with gr.Blocks(title="AskNature BioRAG Expert", theme=gr.themes.Soft()) as demo:
|
| 270 |
-
gr.Markdown("# 🌿 AskNature RAG-based Chatbot
|
| 271 |
with gr.Row():
|
| 272 |
chatbot = gr.Chatbot(label="Dialogue History", height=500)
|
| 273 |
with gr.Row():
|
| 274 |
-
question = gr.Textbox(placeholder="Ask about biomimicry (e.g. 'How does Werewool use coral proteins to make fibers?')",
|
| 275 |
-
|
| 276 |
-
model_selector = gr.Dropdown(choices=["gemini-2.0-flash", "meta-llama-3-70b-instruct"], label="Generation Model", value="gemini-2.0-flash")
|
| 277 |
clear_btn = gr.Button("Clear History", variant="secondary")
|
| 278 |
-
|
| 279 |
gr.Markdown("""
|
| 280 |
<div style="text-align: center; color: #4a7c59;">
|
| 281 |
<small>Powered by AskNature's Database |
|
| 282 |
Explore nature's blueprints at <a href="https://asknature.org">asknature.org</a></small>
|
| 283 |
</div>""")
|
|
|
|
| 284 |
question.submit(chat_interface, [question, chatbot, model_selector], [question, chatbot])
|
| 285 |
clear_btn.click(lambda: [], None, chatbot)
|
| 286 |
|
|
|
|
| 1 |
+
# Combined Gemini Flash and Meta-LLAMA 3 GWDG and Groq Chatbot
|
| 2 |
+
# For Gemini Flash rate limit is 15 requests per minute
|
| 3 |
+
# For Groq rate 30 RPM , 14400 RPD, 6K TPM and 500K TPD
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
import json
|
| 7 |
import logging
|
| 8 |
import re
|
| 9 |
+
from typing import List, Tuple
|
|
|
|
|
|
|
| 10 |
import gradio as gr
|
| 11 |
from openai import OpenAI
|
| 12 |
import google.generativeai as genai
|
| 13 |
+
import requests
|
| 14 |
from functools import lru_cache
|
| 15 |
from tenacity import retry, stop_after_attempt, wait_exponential
|
| 16 |
from langchain_community.retrievers import BM25Retriever
|
| 17 |
from langchain_community.vectorstores import FAISS
|
| 18 |
from langchain_core.embeddings import Embeddings
|
| 19 |
from langchain_core.documents import Document
|
| 20 |
+
from collections import defaultdict
|
|
|
|
| 21 |
import hashlib
|
| 22 |
from tqdm import tqdm
|
| 23 |
from dotenv import load_dotenv
|
|
|
|
| 207 |
context = []
|
| 208 |
for doc in docs:
|
| 209 |
context_str = f"""**Source**: [{doc.metadata['source']}]({doc.metadata['hyperlink']})
|
| 210 |
+
**Application**: {doc.metadata['application']}
|
| 211 |
+
**Key Concepts**: {', '.join(doc.metadata['technical_concepts'])}
|
| 212 |
+
**Strategy Excerpt**:
|
| 213 |
+
{doc.page_content.split('Strategy Excerpt:')[-1].strip()}"""
|
| 214 |
context.append(context_str)
|
| 215 |
return "\n\n---\n\n".join(context)
|
| 216 |
|
| 217 |
# --- Generation System ---
|
| 218 |
+
SYSTEM_PROMPT = """
|
| 219 |
+
**Biomimicry Expert Guidelines**
|
| 220 |
+
|
| 221 |
+
- Use only the provided AskNature context (e.g., Source, Application, Strategy, technical_concepts). If no context is given, note that you're using your own expertise.
|
| 222 |
+
- When referencing facts, use numeric citations in square brackets (e.g., [1]). Do not include full URLs inline.
|
| 223 |
+
- Bold all technical terms (e.g., **protein-based pigmentation**, **DNA-level fiber design**).
|
| 224 |
+
- Provide a concise, expert answer that explains the innovation and its sustainability benefits.
|
| 225 |
+
- End your response with a "References" section listing each URL with its citation number.
|
| 226 |
|
| 227 |
+
Context: {context}
|
| 228 |
+
"""
|
| 229 |
|
| 230 |
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=20))
|
| 231 |
def get_ai_response(query: str, context: str, model: str) -> str:
|
| 232 |
+
result = "" # Initialize the result variable
|
| 233 |
try:
|
| 234 |
if model == "gemini-2.0-flash":
|
| 235 |
gemini_model = genai.GenerativeModel(model)
|
| 236 |
response = gemini_model.generate_content(
|
| 237 |
f"{SYSTEM_PROMPT.format(context=context)}\nQuestion: {query}\nProvide a detailed technical answer:"
|
| 238 |
)
|
| 239 |
+
logger.info(f"Response from gemini-2.0-flash: {response.text}")
|
| 240 |
+
result = _postprocess_response(response.text)
|
| 241 |
elif model == "meta-llama-3-70b-instruct":
|
| 242 |
response = client.chat.completions.create(
|
| 243 |
model=model,
|
|
|
|
| 248 |
temperature=0.4,
|
| 249 |
max_tokens=2000
|
| 250 |
)
|
| 251 |
+
logger.info(f"Response from meta-llama-3-70b-instruct: {response}")
|
| 252 |
+
try:
|
| 253 |
+
result = response.choices[0].message.content
|
| 254 |
+
except Exception as e:
|
| 255 |
+
logger.error(f"Error processing meta-llama-3-70b-instruct response: {str(e)}")
|
| 256 |
+
result = "Failed to process response from meta-llama-3-70b-instruct"
|
| 257 |
+
elif model == "llama3-70b-8192":
|
| 258 |
+
result = get_groq_llama3_response(query)
|
| 259 |
+
logger.info(f"Response from llama3-70b-8192: {result}")
|
| 260 |
+
if result is None:
|
| 261 |
+
result = "Failed to get response from llama3-70b-8192"
|
| 262 |
+
# Append the model name to the response for clarity
|
| 263 |
+
result += f"\n\n**Model:** {model}"
|
| 264 |
+
return result
|
| 265 |
except Exception as e:
|
| 266 |
logger.error(f"Generation Error: {str(e)}")
|
| 267 |
return "I'm unable to generate a response right now. Please try again later."
|
|
|
|
| 271 |
response = re.sub(r"\*\*([\w-]+)\*\*", r"**\1**", response)
|
| 272 |
return response
|
| 273 |
|
| 274 |
+
def get_groq_llama3_response(query: str) -> str:
|
| 275 |
+
"""Get response from Llama 3 on Groq Cloud."""
|
| 276 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 277 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 278 |
+
|
| 279 |
+
headers = {
|
| 280 |
+
"Content-Type": "application/json",
|
| 281 |
+
"Authorization": f"Bearer {api_key}"
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
payload = {
|
| 285 |
+
"model": "llama3-70b-8192",
|
| 286 |
+
"messages": [
|
| 287 |
+
{
|
| 288 |
+
"role": "user",
|
| 289 |
+
"content": query
|
| 290 |
+
}
|
| 291 |
+
]
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
try:
|
| 295 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 296 |
+
response.raise_for_status()
|
| 297 |
+
result = response.json()
|
| 298 |
+
logger.info(f"Groq API Response: {result}")
|
| 299 |
+
return result["choices"][0]["message"]["content"]
|
| 300 |
+
except requests.exceptions.RequestException as e:
|
| 301 |
+
logger.error(f"Groq API Error: {str(e)}")
|
| 302 |
+
return "An error occurred while contacting Groq's Llama 3 model."
|
| 303 |
+
|
| 304 |
# --- Pipeline ---
|
| 305 |
documents = load_and_chunk_data(data_file_name)
|
| 306 |
retriever = EnhancedRetriever(documents)
|
|
|
|
| 319 |
return "", history + [(question, response)]
|
| 320 |
|
| 321 |
with gr.Blocks(title="AskNature BioRAG Expert", theme=gr.themes.Soft()) as demo:
|
| 322 |
+
gr.Markdown("# 🌿 AskNature RAG-based Chatbot")
|
| 323 |
with gr.Row():
|
| 324 |
chatbot = gr.Chatbot(label="Dialogue History", height=500)
|
| 325 |
with gr.Row():
|
| 326 |
+
question = gr.Textbox(placeholder="Ask about biomimicry (e.g. 'How does Werewool use coral proteins to make fibers?')", label="Inquiry", scale=4)
|
| 327 |
+
model_selector = gr.Dropdown(choices=["gemini-2.0-flash", "meta-llama-3-70b-instruct(GWDG)", "llama3-70b-8192(Groq)"], label="Generation Model", value="gemini-2.0-flash")
|
|
|
|
| 328 |
clear_btn = gr.Button("Clear History", variant="secondary")
|
| 329 |
+
|
| 330 |
gr.Markdown("""
|
| 331 |
<div style="text-align: center; color: #4a7c59;">
|
| 332 |
<small>Powered by AskNature's Database |
|
| 333 |
Explore nature's blueprints at <a href="https://asknature.org">asknature.org</a></small>
|
| 334 |
</div>""")
|
| 335 |
+
|
| 336 |
question.submit(chat_interface, [question, chatbot, model_selector], [question, chatbot])
|
| 337 |
clear_btn.click(lambda: [], None, chatbot)
|
| 338 |
|