File size: 1,059 Bytes
2c59309
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import requests
import gradio as gr

API_TOKEN=os.getenv("HF_TOKEN")

headers = {"Authorization":f"Bearer {API_TOKEN}"}
API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-large"

pass_user_inputs=[]
generated_responses=[]

def query(message, history):
    global pass_user_inputs
    global generated_responses

    payload = {"inputs": {
        "past_user_inputs": pass_user_inputs,
        "generated_responses": pass_user_inputs,
        "text": message,
    }}
    try:
        response=requests.post(API_URL, headers=headers, json=payload)
        if response.status_code == 200:
            pass_user_inputs=response.json()['conversation']['past_user_inputs']
            generated_responses=response.json()['conversation']['generated_responses']
            return response.json()['generated_text']
        return "Sorry, the inference API did not complete successfully."
    except Exception as e:
        return "Sorry, internal error. Please refresh the page and try again."


demo=gr.ChatInterface(query).launch()