Spaces:
Sleeping
Sleeping
| from openai import OpenAI | |
| import gradio as gr | |
| import os | |
| from dotenv import load_dotenv | |
| from fetch_all_proteins import * | |
| import json | |
| import requests | |
| load_dotenv() | |
| email = os.getenv("EMAIL") | |
| client = OpenAI( | |
| api_key=os.environ.get("OPENAI_API_KEY"), | |
| ) | |
| def chatApiCall(messages): | |
| payload = { | |
| "messages": messages, | |
| "web_access": False | |
| } | |
| url = "https://open-ai21.p.rapidapi.com/claude3" | |
| headers = { | |
| "x-rapidapi-key": os.environ.get("OPENAI_API_KEY"), | |
| "x-rapidapi-host": "open-ai21.p.rapidapi.com", | |
| "Content-Type": "application/json" | |
| } | |
| # response = requests.post(url, json=payload, headers=headers) | |
| response = client.chat.completions.create( | |
| messages=messages, | |
| model="gpt-4o-mini", | |
| ) | |
| res_json=response.choices[0].message.content | |
| print("response",res_json) | |
| return res_json | |
| ## Create a function that determines whether the request relates to a protein or not | |
| def IsProteinRequest(history, message): | |
| prompt = """Respond only with true when the conditions below are met otherwise respond only with false. The conditions | |
| are as follows: | |
| 1: Within the context of the chat history, the message refers to a specific protein. | |
| 2: A specific protein name is mentioned in the message. GCPR proteins alone does not count. | |
| 3: If there are no proteins mentioned in the chat history, there should be a specific protein name mentioned in the message. | |
| 4: If there are no chat histories, look at the following message. | |
| 5: If you detect any generalized requests like "Tell me about proteins" "Tell me about receptors" or any request that has no | |
| specific protein mentioned like Rhodopsin or OR51E2, respond with false. | |
| The message is as follows: """ + message | |
| history.append({"role":"user", "content": f"{prompt}"}) | |
| # function_client = OpenAI( | |
| # api_key=os.environ.get("OPENAI_API_KEY") | |
| # ) | |
| # function_client = OpenAI() | |
| # response = function_client.chat.completions.create( | |
| # model="gpt-4o-mini", | |
| # messages=history, | |
| # ) | |
| response=chatApiCall(history) | |
| # print("prompt",history,"res",response) | |
| return response | |
| ## Create a function that returns the name of the protein | |
| def ProteinName(history, message): | |
| prompt = """Respond only with the name of the protein the message is referring to with respect to both | |
| the chat history above and the message itself. The message is as follows: """ + message | |
| history.append({"role":"user", "content": f"{prompt}"}) | |
| # function_client = OpenAI( | |
| # api_key=os.environ.get("OPENAI_API_KEY") | |
| # ) | |
| # function_client = OpenAI() | |
| # response = function_client.chat.completions.create( | |
| # model="gpt-4o-mini", | |
| # messages=history, | |
| # ) | |
| response=chatApiCall(history) | |
| # print("prompt protein name",history,"res",response) | |
| return response | |
| ## Create a function that takes in a protein name and returns protein info | |
| def ProteinInfo(protein): | |
| print("caall hua hai",protein) | |
| accession, full_name = fetch_protein_info(protein) | |
| all_data = { | |
| "uniprot": fetch_uniprot_info(accession, email), | |
| "interpro": fetch_comprehensive_interpro_info(accession, email), | |
| # "string": fetch_string_info(accession, 9606, email), # Assuming human (9606) | |
| "quickgo": {} | |
| } | |
| go_terms = fetch_protein_go_terms(accession, email) | |
| all_data["quickgo"]["go_terms"] = go_terms | |
| # for go_term in go_terms: | |
| # all_data["quickgo"][go_term] = fetch_go_info(go_term, email) | |
| # print(all_data) | |
| # with open(f"{protein}.json",'w') as f: | |
| # json.dump(all_data,f) | |
| return json.dumps(all_data) | |
| ## Create a function that takes in a message and a protein information and returns an informed response | |
| def InformedResponse(proteinInfo, message): | |
| prompt = f"{proteinInfo} From the following information given, answer this question: " + message | |
| history=[] | |
| Agent = {"role": "system", "content": "You are a helpful assistant with extensive background in protein analysis."} | |
| history.append(Agent) | |
| history.append({"role":"user", "content": f"{prompt}"}) | |
| # function_client = OpenAI( | |
| # api_key=os.environ.get("OPENAI_API_KEY") | |
| # ) | |
| # function_client = OpenAI() | |
| # response = function_client.chat.completions.create( | |
| # model="gpt-4o-mini", | |
| # messages=history, | |
| # ) | |
| response=chatApiCall(history) | |
| # print("prompt",history,"res",response) | |
| return response | |
| def HistoryConverter(history): | |
| Agent = {"role": "system", "content": "You are a helpful assistant with extensive background in protein analysis."} | |
| formatted_history = [] | |
| formatted_history.append(Agent) | |
| for each in history: | |
| formatted_history.append({"role": "user", "content": f"{each[0]}"}) | |
| formatted_history.append({"role": "assistant", "content": f"{each[1]}"}) | |
| return formatted_history | |
| def openai_chatbot(message, history): | |
| formatted_history = HistoryConverter(history=history) | |
| isProteinRequest = IsProteinRequest(history=formatted_history, message=message) | |
| if isProteinRequest == "true": | |
| proteinName = ProteinName(history=formatted_history, message=message) | |
| proteinInfo = ProteinInfo(protein=proteinName) | |
| print(proteinName,proteinInfo) | |
| return InformedResponse(proteinInfo=proteinInfo, message=message) | |
| else: | |
| # client = OpenAI() | |
| messages = HistoryConverter(history=history) | |
| messages.append({"role":"user","content": f"{message}"}) | |
| # payload = { | |
| # "messages": messages, | |
| # "web_access": False | |
| # } | |
| # response=chatApiCall({"messages":history}) | |
| # response = client.chat.completions.create( | |
| # model="gpt-4o-mini", | |
| # # messages=[{"role":"system", "content": "You are a helpful assistant"}, {"role":"user", "content":"Tell me about peter pan"}] | |
| # messages = messages | |
| # ) | |
| response=chatApiCall(messages) | |
| # print("prompt",messages,"res",response) | |
| return response | |
| if __name__=="__main__": | |
| demo_chatbot = gr.ChatInterface(openai_chatbot, title="ProteinSage", description="Ask anything about proteins, we fetch you data from major protein databases ;)") | |
| demo_chatbot.launch() |