Spaces:
Build error
Build error
Update models_api.py
Browse files- models_api.py +86 -57
models_api.py
CHANGED
|
@@ -1,57 +1,86 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
import json
|
| 3 |
-
import os
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
load_dotenv()
|
| 6 |
-
|
| 7 |
-
def get_answer(model_name, context, question):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
from huggingface_hub import InferenceClient
|
| 52 |
-
|
| 53 |
-
def get_hugging_face_answer(model_name, context, question):
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import requests
|
| 2 |
+
# import json
|
| 3 |
+
# import os
|
| 4 |
+
# from dotenv import load_dotenv
|
| 5 |
+
# load_dotenv()
|
| 6 |
+
|
| 7 |
+
# def get_answer(model_name, context, question):
|
| 8 |
+
# llm_key = os.getenv("llm_key")
|
| 9 |
+
# url = os.getenv("main_url")
|
| 10 |
+
# # Construct the prompt for the model
|
| 11 |
+
# prompt = f"You are a Question Answering Model. Can you help me answer the question: {question} from the context: {context}? Just return the answer only. The document may contain some Arabic text; please translate that to English if needed."
|
| 12 |
+
|
| 13 |
+
# # Prepare payload for API request
|
| 14 |
+
# payload = {
|
| 15 |
+
# "model": model_name,
|
| 16 |
+
# "messages": [
|
| 17 |
+
# {
|
| 18 |
+
# "role": "user",
|
| 19 |
+
# "content": prompt
|
| 20 |
+
# }
|
| 21 |
+
# ],
|
| 22 |
+
# "max_tokens": 300,
|
| 23 |
+
# "temperature": 0.2
|
| 24 |
+
# }
|
| 25 |
+
|
| 26 |
+
# headers = {
|
| 27 |
+
# 'Authorization': f'Bearer {llm_key}',
|
| 28 |
+
# 'Content-Type': 'application/json'
|
| 29 |
+
# }
|
| 30 |
+
|
| 31 |
+
# # Convert payload to JSON string
|
| 32 |
+
# json_payload = json.dumps(payload)
|
| 33 |
+
|
| 34 |
+
# try:
|
| 35 |
+
# # Send POST request to the API
|
| 36 |
+
# response = requests.post(url, headers=headers, data=json_payload)
|
| 37 |
+
|
| 38 |
+
# # Check if request was successful
|
| 39 |
+
# if response.status_code == 200:
|
| 40 |
+
# response_data = response.json() # Parse response JSON
|
| 41 |
+
# answer = response_data['choices'][0]['message']['content'] # Extract model's answer from response
|
| 42 |
+
# return answer
|
| 43 |
+
# else:
|
| 44 |
+
# print(f"Request failed with status code: {response.status_code}")
|
| 45 |
+
# return None
|
| 46 |
+
|
| 47 |
+
# except requests.exceptions.RequestException as e:
|
| 48 |
+
# print(f"Error occurred: {e}")
|
| 49 |
+
# return None
|
| 50 |
+
|
| 51 |
+
# from huggingface_hub import InferenceClient
|
| 52 |
+
|
| 53 |
+
# def get_hugging_face_answer(model_name, context, question):
|
| 54 |
+
# client = InferenceClient(model_name, token=os.getenv("HF_TOKEN"))
|
| 55 |
+
# prompt = f"You are a Question Answering Model. Can you help me answer the question: {question} from the context: {context}? Just return the answer only. The document may contain some Arabic text; please translate that to English if needed."
|
| 56 |
+
# output = client.text_generation(prompt , max_new_tokens = 200, stream=True, temperature=0.1)
|
| 57 |
+
# return output
|
| 58 |
+
|
| 59 |
+
import os
|
| 60 |
+
from groq import Groq
|
| 61 |
+
from dotenv import load_dotenv
|
| 62 |
+
load_dotenv()
|
| 63 |
+
|
| 64 |
+
GROQ_API_KEY = os.getenv('GROQ_API')
|
| 65 |
+
def get_answer_from_context(model_name, context, question):
|
| 66 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 67 |
+
chat_completion = client.chat.completions.create(
|
| 68 |
+
model=model_name,
|
| 69 |
+
messages=[
|
| 70 |
+
{
|
| 71 |
+
"role": "system",
|
| 72 |
+
"content": f"You are a Question Answering LLM that uses context provided: {context} to answer user's query. Just return the answer only. The document may contain some Arabic text; please translate that to English if needed."
|
| 73 |
+
},
|
| 74 |
+
{
|
| 75 |
+
"role": "user",
|
| 76 |
+
"content": question,
|
| 77 |
+
}
|
| 78 |
+
],
|
| 79 |
+
temperature=0.2,
|
| 80 |
+
max_tokens=200,
|
| 81 |
+
top_p=1,
|
| 82 |
+
stop=None,
|
| 83 |
+
stream=False,
|
| 84 |
+
# response_format={"type": "json_object"}
|
| 85 |
+
)
|
| 86 |
+
return chat_completion.choices[0].message.content
|