lyimo commited on
Commit
a52de3d
·
verified ·
1 Parent(s): 18b922b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -18
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- import requests
3
  from sentence_transformers import SentenceTransformer
4
  from sklearn.metrics.pairwise import cosine_similarity
5
  import numpy as np
@@ -15,24 +15,16 @@ df = pd.read_csv('combined_questions_and_answers.csv')
15
  # Encode all questions in the dataset
16
  question_embeddings = model.encode(df['Question'].tolist())
17
 
18
- # Hugging Face API details for Meta-Llama 70B
19
- API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B"
20
- headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
21
 
22
- # Function to call Hugging Face API to refine and translate text
23
  def refine_text(prompt):
24
- payload = {
25
- "inputs": prompt,
26
- "parameters": {
27
- "max_new_tokens": 800,
28
- "temperature": 0.7
29
- }
30
- }
31
- response = requests.post(API_URL, headers=headers, json=payload)
32
- response_json = response.json()
33
- if isinstance(response_json, list) and len(response_json) > 0:
34
- return response_json[0].get('generated_text', '')
35
- return "Error in refining text."
36
 
37
  # Function to find the most similar question and provide the answer
38
  def get_answer(user_question, threshold=0.30):
@@ -49,7 +41,7 @@ def get_answer(user_question, threshold=0.30):
49
  similar_question_idx = np.argmax(similarities)
50
  # Retrieve the corresponding answer
51
  answer = df.iloc[similar_question_idx]['Answer']
52
- # Refine the answer using Meta-Llama 70B
53
  refined_answer = refine_text(f"Refine this answer: {answer}")
54
  return refined_answer, max_similarity
55
  else:
 
1
  import os
2
+ from transformers import pipeline
3
  from sentence_transformers import SentenceTransformer
4
  from sklearn.metrics.pairwise import cosine_similarity
5
  import numpy as np
 
15
  # Encode all questions in the dataset
16
  question_embeddings = model.encode(df['Question'].tolist())
17
 
18
+ # Hugging Face API details for Meta-Llama 3B
19
+ pipe = pipeline("text-generation", model="meta-llama/Meta-Llama-3-8B-Instruct")
 
20
 
21
+ # Function to refine and translate text using Meta-Llama 3B
22
  def refine_text(prompt):
23
+ messages = [
24
+ {"role": "user", "content": prompt},
25
+ ]
26
+ response = pipe(messages)
27
+ return response[0]['generated_text']
 
 
 
 
 
 
 
28
 
29
  # Function to find the most similar question and provide the answer
30
  def get_answer(user_question, threshold=0.30):
 
41
  similar_question_idx = np.argmax(similarities)
42
  # Retrieve the corresponding answer
43
  answer = df.iloc[similar_question_idx]['Answer']
44
+ # Refine the answer using Meta-Llama 3B
45
  refined_answer = refine_text(f"Refine this answer: {answer}")
46
  return refined_answer, max_similarity
47
  else: