xcurv commited on
Commit
bb0fd16
·
verified ·
1 Parent(s): e9b1dea

Update app.py

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