File size: 1,250 Bytes
35041c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52bdf51
 
35041c8
52bdf51
35041c8
203af46
 
35041c8
52bdf51
35041c8
 
 
52bdf51
 
 
 
203af46
f03f79e
35041c8
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
import gradio as gr
import requests

API_URL = "https://api-inference.huggingface.co/models/tiiuae/falcon-7b-instruct"
headers = {"Authorization": "Bearer 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"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

API_URL2 = "https://api-inference.huggingface.co/models/valhalla/longformer-base-4096-finetuned-squadv1"
headers2 = {"Authorization": "Bearer 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"}

def query2(payload):
    response = requests.post(API_URL2, headers=headers2, json=payload)
    return response.json()

def get_context_func(question, context_output):
    payload = {"question": question}
    result = query(payload)
    context_output.update(result['answer'])

def ask_ai(question, context_output, answer_output):
    payload = {"question": question, "context": context_output.value}
    result = query2(payload)
    answer_output.update(result['answer'])

iface = gr.Interface(
    fn=ask_ai,
    inputs=[
        gr.Textbox("Question"),
        gr.Textbox("Context"),
        gr.Button("get_context", get_context_func),
        gr.Button("ask_ai", live=False) # Setting live to False for manual control of updates
    ]
iface.launch()