Spaces:
Sleeping
Sleeping
David
commited on
Commit
·
59b000d
1
Parent(s):
f7d8d0c
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
# Set up the API endpoint and key
|
| 6 |
-
API_URL = os.getenv("RUNPOD_API_URL")
|
| 7 |
-
API_KEY = os.getenv("RUNPOD_API_KEY")
|
| 8 |
|
| 9 |
headers = {
|
| 10 |
"Authorization": f"Bearer {API_KEY}",
|
|
@@ -24,7 +25,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 24 |
messages.append({"role": "user", "content": message})
|
| 25 |
|
| 26 |
data = {
|
| 27 |
-
"model": "forcemultiplier/fmx-reflective-2b",
|
| 28 |
"messages": messages,
|
| 29 |
"max_tokens": max_tokens,
|
| 30 |
"temperature": temperature,
|
|
@@ -32,10 +33,18 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 32 |
}
|
| 33 |
|
| 34 |
try:
|
|
|
|
|
|
|
|
|
|
| 35 |
response = requests.post(API_URL, headers=headers, json=data)
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
response_json = response.json()
|
|
|
|
| 39 |
|
| 40 |
if 'choices' in response_json and len(response_json['choices']) > 0:
|
| 41 |
return response_json['choices'][0]['message']['content']
|
|
@@ -43,32 +52,28 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 43 |
return f"Error: Unexpected response format. Full response: {response_json}"
|
| 44 |
|
| 45 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 46 |
return f"Error: {str(e)}"
|
| 47 |
except ValueError as e:
|
|
|
|
| 48 |
return f"Error: Invalid JSON response. {str(e)}"
|
| 49 |
except KeyError as e:
|
|
|
|
| 50 |
return f"Error: Unexpected response structure. Missing key: {str(e)}"
|
| 51 |
except Exception as e:
|
|
|
|
| 52 |
return f"Unexpected error: {str(e)}"
|
| 53 |
|
| 54 |
demo = gr.ChatInterface(
|
| 55 |
respond,
|
| 56 |
additional_inputs=[
|
| 57 |
-
gr.Textbox(
|
| 58 |
-
# value="You are an advanced artificial intelligence system, capable of <thinking> <reflection> and you output a brief and to-the-point <output>.",
|
| 59 |
-
label="System message"
|
| 60 |
-
),
|
| 61 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens"),
|
| 62 |
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 63 |
-
gr.Slider(
|
| 64 |
-
minimum=0.1,
|
| 65 |
-
maximum=1.0,
|
| 66 |
-
value=0.95,
|
| 67 |
-
step=0.05,
|
| 68 |
-
label="Top-p (nucleus sampling)",
|
| 69 |
-
),
|
| 70 |
],
|
| 71 |
)
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
|
|
|
| 74 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
# Set up the API endpoint and key
|
| 7 |
+
API_URL = os.getenv("RUNPOD_API_URL")
|
| 8 |
+
API_KEY = os.getenv("RUNPOD_API_KEY")
|
| 9 |
|
| 10 |
headers = {
|
| 11 |
"Authorization": f"Bearer {API_KEY}",
|
|
|
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
|
| 27 |
data = {
|
| 28 |
+
"model": "forcemultiplier/fmx-reflective-2b",
|
| 29 |
"messages": messages,
|
| 30 |
"max_tokens": max_tokens,
|
| 31 |
"temperature": temperature,
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
try:
|
| 36 |
+
print(f"Sending request to API: {API_URL}")
|
| 37 |
+
print(f"Request data: {json.dumps(data, indent=2)}")
|
| 38 |
+
|
| 39 |
response = requests.post(API_URL, headers=headers, json=data)
|
| 40 |
+
|
| 41 |
+
print(f"Raw API Response: {response.text}")
|
| 42 |
+
print(f"Response Status Code: {response.status_code}")
|
| 43 |
+
|
| 44 |
+
response.raise_for_status()
|
| 45 |
|
| 46 |
response_json = response.json()
|
| 47 |
+
print(f"Formatted API Response: {json.dumps(response_json, indent=2)}")
|
| 48 |
|
| 49 |
if 'choices' in response_json and len(response_json['choices']) > 0:
|
| 50 |
return response_json['choices'][0]['message']['content']
|
|
|
|
| 52 |
return f"Error: Unexpected response format. Full response: {response_json}"
|
| 53 |
|
| 54 |
except requests.exceptions.RequestException as e:
|
| 55 |
+
print(f"Request Exception: {str(e)}")
|
| 56 |
return f"Error: {str(e)}"
|
| 57 |
except ValueError as e:
|
| 58 |
+
print(f"JSON Parsing Error: {str(e)}")
|
| 59 |
return f"Error: Invalid JSON response. {str(e)}"
|
| 60 |
except KeyError as e:
|
| 61 |
+
print(f"Key Error: {str(e)}")
|
| 62 |
return f"Error: Unexpected response structure. Missing key: {str(e)}"
|
| 63 |
except Exception as e:
|
| 64 |
+
print(f"Unexpected Error: {str(e)}")
|
| 65 |
return f"Unexpected error: {str(e)}"
|
| 66 |
|
| 67 |
demo = gr.ChatInterface(
|
| 68 |
respond,
|
| 69 |
additional_inputs=[
|
| 70 |
+
gr.Textbox(label="System message"),
|
|
|
|
|
|
|
|
|
|
| 71 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens"),
|
| 72 |
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 73 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
],
|
| 75 |
)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
print(f"Starting application with API URL: {API_URL}")
|
| 79 |
demo.launch()
|