|
import os |
|
import numpy as np |
|
import pandas as pd |
|
import openai |
|
from haystack.schema import Document |
|
import streamlit as st |
|
from tenacity import retry, stop_after_attempt, wait_random_exponential |
|
from huggingface_hub import InferenceClient |
|
|
|
|
|
|
|
hf_token = os.environ["HF_API_KEY"] |
|
|
|
|
|
def get_prompt(context, label): |
|
base_prompt="Summarize the following context efficiently in bullet points, the less the better - but keep concrete goals. \ |
|
Summarize only elements of the context that address vulnerability of "+label+" to climate change. \ |
|
If there is no mention of "+label+" in the context, return: 'No clear references to vulnerability of "+label+" found'. \ |
|
Do not include an introduction sentence, just the bullet points as per below. \ |
|
Formatting example: \ |
|
- Bullet point 1 \ |
|
- Bullet point 2 \ |
|
" |
|
|
|
prompt = base_prompt+"; Context: "+context+"; Answer:" |
|
|
|
return prompt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChatCompletionResult: |
|
def __init__(self): |
|
self.content = "" |
|
|
|
def add_content(self, text): |
|
self.content += text |
|
|
|
def get_full_content(self): |
|
return self.content.strip() |
|
|
|
def run_query(context, label, model_sel_name): |
|
''' |
|
Summarize provided test |
|
''' |
|
chatbot_role = """You are an analyst specializing in climate change impact assessments and producing insights from policy documents.""" |
|
messages = [{"role": "system", "content": chatbot_role},{"role": "user", "content": get_prompt(context, label)}] |
|
|
|
|
|
client = InferenceClient(model_sel_name, token=hf_token) |
|
|
|
|
|
chat_completion = client.chat.completions.create( |
|
messages=messages, |
|
stream=True |
|
) |
|
|
|
|
|
completion_result = ChatCompletionResult() |
|
res_box = st.empty() |
|
|
|
|
|
for chunk in chat_completion: |
|
|
|
if chunk.choices is not None: |
|
chunk_message = chunk.choices[0].delta |
|
if 'content' in chunk_message: |
|
completion_result.add_content(chunk_message['content']) |
|
|
|
result = completion_result.get_full_content() |
|
res_box.success(result) |
|
|
|
|
|
return completion_result |
|
|
|
|
|
|
|
|
|
|