Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,17 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
|
4 |
-
|
5 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
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()
|