-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuggingface_api.py
More file actions
37 lines (29 loc) · 1.01 KB
/
huggingface_api.py
File metadata and controls
37 lines (29 loc) · 1.01 KB
1
2
3
4
5
6
7
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
from huggingface_hub import InferenceClient
from dotenv import load_dotenv
import os
import requests
class HuggingFaceAPI:
def __init__(self):
load_dotenv()
self.api_key = os.getenv("HUGGINGFACE_API_KEY")
def analyze_text(self, text, prompt):
try:
client = InferenceClient(api_key=self.api_key)
messages = [
{
"role": "system",
"content": prompt
},
{
"role": "user",
"content": f"Aufgabe: Analysiere den hochgeladenen Arztbrief: {text}"
}
]
completion = client.chat.completions.create(
model="Qwen/QwQ-32B-Preview",
messages=messages,
max_tokens=500
)
return completion.choices[0].message.content
except requests.exceptions.RequestException as e:
return f"Fehler bei der API-Anfrage: {str(e)}"