pretzinger commited on
Commit
94cbad2
·
1 Parent(s): 1d9da65

Fixed divergent branch

Browse files
Files changed (1) hide show
  1. app.py +30 -24
app.py CHANGED
@@ -7,6 +7,7 @@ import torch
7
  import faiss
8
  import numpy as np
9
  import json
 
10
 
11
  def clean_payload(payload):
12
  # Remove "data:" prefix and clean newline characters
@@ -18,11 +19,18 @@ def clean_payload(payload):
18
  json_payload = None
19
  return json_payload
20
 
 
 
 
 
 
 
 
21
 
22
  # API Keys and Org ID
23
  openai.api_key = os.getenv("OPENAI_API_KEY")
24
  openai.organization = os.getenv("OPENAI_ORG_ID")
25
- serper_api_key = os.getenv("SERPER_API_KEY")
26
 
27
  # Load PubMedBERT tokenizer and model
28
  tokenizer = BertTokenizer.from_pretrained("microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract")
@@ -32,37 +40,29 @@ model = BertForSequenceClassification.from_pretrained("microsoft/BiomedNLP-PubMe
32
  dimension = 768
33
  index = faiss.IndexFlatL2(dimension)
34
 
35
- # Function to embed text (PubMedBERT)
36
  def embed_text(text):
37
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding="max_length", max_length=512)
38
  outputs = model(**inputs, output_hidden_states=True)
39
- hidden_state = outputs.hidden_states[-1]
40
- return hidden_state.mean(dim=1).detach().numpy()
41
 
42
- # Function to retrieve info from PubMedBERT
43
  def handle_fda_query(query):
44
  inputs = tokenizer(query, return_tensors="pt", padding="max_length", truncation=True, max_length=512)
45
- outputs = model(**inputs)
46
- logits = outputs.logits
47
- prediction = torch.argmax(logits, dim=1).item()
48
-
49
- # Simulate a meaningful FDA-related response
50
- if prediction == 1:
51
- return f"FDA Query Processed: '{query}' contains important regulatory information."
52
- else:
53
- return f"FDA Query Processed: '{query}' seems to be general and not regulatory-heavy."
54
 
55
- # Function to enhance info via GPT-4o-mini
56
  def enhance_with_gpt4o(fda_response):
57
- try:
58
- response = openai.ChatCompletion.create(
59
- model="gpt-4o-mini", # Correct model
60
- messages=[{"role": "system", "content": "You are an expert FDA assistant."}, {"role": "user", "content": f"Enhance this FDA info: {fda_response}"}],
61
- max_tokens=150
62
- )
63
- return response['choices'][0]['message']['content']
64
- except Exception as e:
65
- return f"Error: {str(e)}"
66
 
67
  def respond(message, system_message, max_tokens, temperature, top_p):
68
  try:
@@ -71,9 +71,15 @@ def respond(message, system_message, max_tokens, temperature, top_p):
71
 
72
  # Stream the enhanced response via GPT-4o-mini using the client
73
  enhanced_response = ""
 
74
  for chat_message in client.chat_completion(...):
75
  payload = json.loads(chat_message.lstrip("data:").rstrip("\n"))
76
  enhanced_response += payload["content"] # Or however the payload structure works
 
 
 
 
 
77
 
78
  # Return both the PubMedBERT result and the enhanced version
79
  return f"Original Info from PubMedBERT: {fda_response}\n\nEnhanced Info via GPT-4o-mini: {enhanced_response}"
 
7
  import faiss
8
  import numpy as np
9
  import json
10
+ <<<<<<< Updated upstream
11
 
12
  def clean_payload(payload):
13
  # Remove "data:" prefix and clean newline characters
 
19
  json_payload = None
20
  return json_payload
21
 
22
+ =======
23
+ from huggingface_hub import InferenceClient # Keeping Hugging Face Client as requested
24
+
25
+ def clean_payload(payload):
26
+ cleaned_payload = payload.lstrip("data:").rstrip("\n")
27
+ return json.loads(cleaned_payload)
28
+ >>>>>>> Stashed changes
29
 
30
  # API Keys and Org ID
31
  openai.api_key = os.getenv("OPENAI_API_KEY")
32
  openai.organization = os.getenv("OPENAI_ORG_ID")
33
+ serper_api_key = os.getenv("SERPER_API_KEY") # SERPER API key from environment variable
34
 
35
  # Load PubMedBERT tokenizer and model
36
  tokenizer = BertTokenizer.from_pretrained("microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract")
 
40
  dimension = 768
41
  index = faiss.IndexFlatL2(dimension)
42
 
43
+ # Embed text (PubMedBERT)
44
  def embed_text(text):
45
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding="max_length", max_length=512)
46
  outputs = model(**inputs, output_hidden_states=True)
47
+ return outputs.hidden_states[-1].mean(dim=1).detach().numpy()
 
48
 
49
+ # Handle FDA query
50
  def handle_fda_query(query):
51
  inputs = tokenizer(query, return_tensors="pt", padding="max_length", truncation=True, max_length=512)
52
+ logits = model(**inputs).logits
53
+ return "FDA Query Processed: Contains regulatory info." if torch.argmax(logits, dim=1).item() == 1 else "FDA Query Processed: General."
 
 
 
 
 
 
 
54
 
55
+ # Enhance via GPT-4o-mini
56
  def enhance_with_gpt4o(fda_response):
57
+ response = openai.ChatCompletion.create(
58
+ model="gpt-4o-mini",
59
+ messages=[{"role": "system", "content": "You are an expert FDA assistant."}, {"role": "user", "content": f"Enhance this FDA info: {fda_response}"}],
60
+ max_tokens=150
61
+ )
62
+ return response['choices'][0]['message']['content']
63
+
64
+ # Initialize Hugging Face Inference Client
65
+ client = InferenceClient("gpt-4o-mini") # Correct model, make sure to replace with your actual model
66
 
67
  def respond(message, system_message, max_tokens, temperature, top_p):
68
  try:
 
71
 
72
  # Stream the enhanced response via GPT-4o-mini using the client
73
  enhanced_response = ""
74
+ <<<<<<< Updated upstream
75
  for chat_message in client.chat_completion(...):
76
  payload = json.loads(chat_message.lstrip("data:").rstrip("\n"))
77
  enhanced_response += payload["content"] # Or however the payload structure works
78
+ =======
79
+ for chat_message in client.chat_completion(...): # Add params if needed
80
+ payload = clean_payload(chat_message)
81
+ enhanced_response += payload["content"]
82
+ >>>>>>> Stashed changes
83
 
84
  # Return both the PubMedBERT result and the enhanced version
85
  return f"Original Info from PubMedBERT: {fda_response}\n\nEnhanced Info via GPT-4o-mini: {enhanced_response}"