Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| import json | |
| # Set up the API endpoint and key | |
| API_URL = os.getenv("RUNPOD_API_URL") | |
| API_KEY = os.getenv("RUNPOD_API_KEY") | |
| headers = { | |
| "Authorization": f"Bearer {API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| def respond(message, history, system_message, max_tokens, temperature, top_p): | |
| if system_message is not None: | |
| messages = [{"role": "system", "content": system_message}] | |
| else: | |
| messages = [] | |
| for human, assistant in history: | |
| messages.append({"role": "user", "content": human}) | |
| messages.append({"role": "assistant", "content": assistant}) | |
| messages.append({"role": "user", "content": message}) | |
| data = { | |
| "model": "forcemultiplier/fmx-reflective-2b", | |
| "messages": messages, | |
| "max_tokens": max_tokens, | |
| "temperature": temperature, | |
| "top_p": top_p | |
| } | |
| try: | |
| print(f"Sending request to API: {API_URL}") | |
| print(f"Request data: {json.dumps(data, indent=2)}") | |
| response = requests.post(API_URL, headers=headers, json=data) | |
| print(f"Raw API Response: {response.text}") | |
| print(f"Response Status Code: {response.status_code}") | |
| response.raise_for_status() | |
| response_json = response.json() | |
| print(f"Formatted API Response: {json.dumps(response_json, indent=2)}") | |
| if 'choices' in response_json and len(response_json['choices']) > 0: | |
| return response_json['choices'][0]['message']['content'] | |
| else: | |
| return f"Error: Unexpected response format. Full response: {response_json}" | |
| except requests.exceptions.RequestException as e: | |
| print(f"Request Exception: {str(e)}") | |
| return f"Error: {str(e)}" | |
| except ValueError as e: | |
| print(f"JSON Parsing Error: {str(e)}") | |
| return f"Error: Invalid JSON response. {str(e)}" | |
| except KeyError as e: | |
| print(f"Key Error: {str(e)}") | |
| return f"Error: Unexpected response structure. Missing key: {str(e)}" | |
| except Exception as e: | |
| print(f"Unexpected Error: {str(e)}") | |
| return f"Unexpected error: {str(e)}" | |
| demo = gr.ChatInterface( | |
| respond, | |
| additional_inputs=[ | |
| gr.Textbox(label="System message"), | |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens"), | |
| gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"), | |
| ], | |
| ) | |
| if __name__ == "__main__": | |
| print(f"Starting application with API URL: {API_URL}") | |
| demo.launch() |