File size: 2,128 Bytes
b7a288a
a284b3d
 
a1c13b0
 
a206339
b7a288a
a284b3d
 
 
b7a288a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1c13b0
3c53f26
b7a288a
 
 
 
 
 
 
 
 
 
4b29047
b7a288a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import gradio as gr
import requests
import os
from IPython.display import display
from IPython.display import Markdown

api_key = os.environ.get("HYPERBOLIC_API_KEY")
url = "https://api.hyperbolic.xyz/v1/chat/completions"

def hyperbolic(prompt):

    try:
        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {api_key}"
        }
        data = {
            "messages": [
                {
                    "role": "user",
                    "content": f"{prompt}"
                }
            ],
            "model": "Qwen/Qwen2.5-72B-Instruct",
            "max_tokens": 32768,
            "temperature": 0.2,
            "top_p": 0.9
        }

        response = requests.post(url, headers=headers, json=data)
        response.raise_for_status()  # Raise HTTPError for bad responses (4xx or 5xx)
        json_response = response.json()

        # Extract the response text from the JSON
        if 'choices' in json_response and len(json_response['choices']) > 0:
            return json_response['choices'][0]['message']['content']
        else:
            return f"Error: Unexpected response format from Hyperbolic API: {json_response}"

    except requests.exceptions.RequestException as e:
        return f"Error: Could not connect to Hyperbolic API: {e}"
    except (KeyError, TypeError) as e:
        return f"Error: Problem parsing Hyperbolic API response: {e}"
    except Exception as e:
        return f"An unexpected error occurred: {e}"


def chat(message, history):

    bot_message = hyperbolic(message)
    history.append((message, bot_message))
    display(Markdown(bot_message))
    #return bot_message
    return history

if __name__ == '__main__':
    if not api_key:
        print("Error: HYPERBOLIC_API_KEY environment variable not set.  Please set it before running.")
    else:
        demo = gr.ChatInterface(
            fn=chat,
            title="Hyperbolic Chatbot",
            description="A chatbot powered by the Hyperbolic API.",
            chatbot=gr.Chatbot(height=400)  # Adjust height as needed
        )
        demo.launch()