ssbagpcm's picture
Upload folder using huggingface_hub
960bb66 verified
import requests
import json
def fetch_ai_response():
url = "https://api.deepinfra.com/v1/openai/chat/completions"
headers = {
"Accept-Language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
"Connection": "keep-alive",
"Content-Type": "application/json",
"Origin": "https://deepinfra.com",
"Referer": "https://deepinfra.com/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
"User-Agent": "Mozilla/5.0 (Linux; Android 11.0; Surface Duo) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Mobile Safari/537.36",
"X-Deepinfra-Source": "model-embed",
"accept": "text/event-stream",
"sec-ch-ua": "\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Google Chrome\";v=\"128\"",
"sec-ch-ua-mobile": "?1",
"sec-ch-ua-platform": "\"Android\""
}
data = {
"model": "meta-llama/Meta-Llama-3.1-405B-Instruct",
"messages": [{"role": "user", "content": "c'est quoi un trou noir ?"}],
"temperature": 0.1,
"max_tokens": 100000,
"stream": True
}
response = requests.post(url, headers=headers, json=data, stream=True)
if response.status_code != 200:
print(f"Erreur lors de la requête : {response.status_code}")
return
# Initialisation des variables
full_text = []
output_size = 0
for line in response.iter_lines():
if line:
try:
decoded_line = line.decode('utf-8')
if decoded_line.startswith('data:'):
json_data = decoded_line[5:].strip()
if json_data == '[DONE]':
break
parsed_data = json.loads(json_data)
# Extraction du texte
choices = parsed_data.get("choices", [])
for choice in choices:
delta = choice.get("delta", {})
content = delta.get("content", "")
if content:
full_text.append(content)
output_size += len(content)
except json.JSONDecodeError:
continue
# Affichage du texte complet et des informations supplémentaires
complete_text = ''.join(full_text)
print(complete_text)
if __name__ == "__main__":
fetch_ai_response()