xcurv commited on
Commit
e9b1dea
1 Parent(s): 8b7f012

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -22
app.py CHANGED
@@ -1,12 +1,17 @@
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
9
 
 
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
@@ -15,34 +20,45 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
 
18
  messages = [{"role": "system", "content": system_message}]
19
-
20
  for val in history:
21
  if val[0]:
22
  messages.append({"role": "user", "content": val[0]})
23
  if val[1]:
24
  messages.append({"role": "assistant", "content": val[1]})
25
-
26
  messages.append({"role": "user", "content": message})
27
 
28
- response = ""
 
 
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
 
38
 
39
- response += token
40
- yield response
41
 
 
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
@@ -59,6 +75,5 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
+ import os
2
+ import requests
3
  import gradio as gr
4
+ from dotenv import load_dotenv
5
 
6
+ # Load environment variables from .env file
7
+ load_dotenv()
 
 
8
 
9
+ # Fetch token from environment variables
10
+ CLOUDFLARE_TOKEN = os.getenv("CLOUDFLARE_TOKEN")
11
+ if not CLOUDFLARE_TOKEN:
12
+ raise EnvironmentError("CLOUDFLARE_TOKEN not found in environment variables.")
13
 
14
+ # Function to send API requests to the Gemma model
15
  def respond(
16
  message,
17
  history: list[tuple[str, str]],
 
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
 
62
  demo = gr.ChatInterface(
63
  respond,
64
  additional_inputs=[
 
75
  ],
76
  )
77
 
 
78
  if __name__ == "__main__":
79
  demo.launch()