Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,42 +20,45 @@ def respond(
|
|
20 |
temperature,
|
21 |
top_p,
|
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 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
59 |
|
60 |
|
61 |
# Gradio Interface
|
|
|
20 |
temperature,
|
21 |
top_p,
|
22 |
):
|
23 |
+
try:
|
24 |
+
# Construct the messages payload
|
25 |
+
messages = [{"role": "system", "content": system_message}]
|
26 |
+
for val in history:
|
27 |
+
if val[0]:
|
28 |
+
messages.append({"role": "user", "content": val[0]})
|
29 |
+
if val[1]:
|
30 |
+
messages.append({"role": "assistant", "content": val[1]})
|
31 |
+
messages.append({"role": "user", "content": message})
|
32 |
|
33 |
+
# Define API endpoint and headers
|
34 |
+
url = "https://api.cloudflare.com/client/v4/accounts/e16531aac7469b4b54ef1e8108e93495/ai/run/@cf/google/gemma-2b-it-lora"
|
35 |
+
headers = {
|
36 |
+
"Authorization": f"Bearer {CLOUDFLARE_TOKEN}",
|
37 |
+
"Content-Type": "application/json",
|
38 |
+
}
|
39 |
|
40 |
+
# Payload with model settings and messages
|
41 |
+
payload = {
|
42 |
+
"messages": messages,
|
43 |
+
"raw": "true",
|
44 |
+
"lora": "1b3c4e5c-ba8a-4e98-973b-573c572cfb34",
|
45 |
+
"max_tokens": max_tokens,
|
46 |
+
"temperature": temperature,
|
47 |
+
"top_p": top_p,
|
48 |
+
}
|
49 |
|
50 |
+
# Make the API request
|
51 |
+
response = requests.post(url, headers=headers, json=payload)
|
52 |
|
53 |
+
if response.status_code == 200:
|
54 |
+
data = response.json()
|
55 |
+
# Extract response content
|
56 |
+
result = data.get("result", {}).get("response", "No response found.")
|
57 |
+
return result
|
58 |
+
else:
|
59 |
+
return f"Error: {response.status_code} - {response.text}"
|
60 |
+
except Exception as e:
|
61 |
+
return f"An error occurred: {str(e)}"
|
62 |
|
63 |
|
64 |
# Gradio Interface
|