File size: 1,683 Bytes
f782c07
61febee
f782c07
61febee
 
 
 
 
 
 
 
 
 
 
 
 
 
84fef76
 
61febee
fc3da96
61febee
84fef76
1619dde
84fef76
 
 
61febee
 
 
1619dde
61febee
 
84fef76
61febee
2613527
fc3da96
2613527
84fef76
 
ecdb02d
 
2613527
84fef76
2613527
 
 
 
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
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 detect_context_from_question(inputs, outputs):
    question = inputs["a"]
    output = query({
        "inputs": f"context for '{question}' is:",
    })
    inputs["b"].value = output  # Update the value of the context Textbox

def ask_question(inputs, outputs):
    question = inputs["a"]
    context = inputs["b"]
    output2 = query2({
        "inputs": {
            "question": question,
            "context": context
        },
    })
    outputs["answer"].value = output2  # Update the value of the answer Textbox

iface = gr.Interface(
    fn=ask_question,
    inputs=[
        gr.Textbox("Enter your question", type="text", placeholder="Enter your question", name="a"),
        gr.Textbox("Enter context", type="text", placeholder="Enter context", name="b"),
        gr.Button(detect_context_from_question, text="Detect Context", name="detect_button"),
        gr.Button("Ask", name="ask_button")
    ],
    outputs=gr.Textbox("Answer", type="text", placeholder="Answer", name="answer"),  # Single Textbox for the answer
    live=True
)

iface.launch()