wop commited on
Commit
0e7f1ae
1 Parent(s): 506adad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -50
app.py CHANGED
@@ -1,12 +1,6 @@
1
- import gradio as gr
2
- import requests
3
 
4
- API_URL = "https://api-inference.huggingface.co/models/tiiuae/falcon-7b-instruct"
5
- headers = {"Authorization": "Bearer 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"}
6
-
7
- def query(payload):
8
- response = requests.post(API_URL, headers=headers, json=payload)
9
- return response.json()
10
 
11
  API_URL2 = "https://api-inference.huggingface.co/models/valhalla/longformer-base-4096-finetuned-squadv1"
12
  headers2 = {"Authorization": "Bearer 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"}
@@ -15,45 +9,20 @@ def query2(payload):
15
  response = requests.post(API_URL2, headers=headers2, json=payload)
16
  return response.json()
17
 
18
- def ask_question(question, context):
19
- output2 = query2({
20
- "inputs": {
21
- "question": question,
22
- "context": context
23
- },
24
- })
25
- return output2
26
-
27
- iface_ask = gr.Interface(
28
- fn=ask_question,
29
- inputs=[
30
- gr.Textbox(type="text", placeholder="Enter your question"),
31
- gr.Textbox(type="text", placeholder="Enter context"),
32
- gr.Button("Ask", onclick="update_output()") # Use custom JavaScript function
33
- ],
34
- outputs=gr.Textbox(type="text", placeholder="Answer"), # Single Textbox for the answer
35
- live=False # Set live to False
36
- )
37
-
38
- # Define the JavaScript function
39
- javascript_code = """
40
- function update_output() {
41
- var question = gr("#text-input-0").val();
42
- var context = gr("#text-input-1").val();
43
-
44
- // Call the backend function with the updated values
45
- gr('iframe').contentWindow.postMessage({
46
- 'task': 'updateOutput',
47
- 'data': {
48
- 'question': question,
49
- 'context': context
50
- }
51
- }, '*');
52
- }
53
- """
54
-
55
- # Attach the JavaScript code to the interface
56
- iface_ask.inferface.html("head", f"<script>{javascript_code}</script>")
57
-
58
- iface_ask.launch()
59
-
 
1
+ from flask import Flask, render_template, request
 
2
 
3
+ app = Flask(__name__)
 
 
 
 
 
4
 
5
  API_URL2 = "https://api-inference.huggingface.co/models/valhalla/longformer-base-4096-finetuned-squadv1"
6
  headers2 = {"Authorization": "Bearer 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"}
 
9
  response = requests.post(API_URL2, headers=headers2, json=payload)
10
  return response.json()
11
 
12
+ @app.route("/", methods=["GET", "POST"])
13
+ def home():
14
+ answer = ""
15
+ if request.method == "POST":
16
+ question = request.form["question"]
17
+ context = request.form["context"]
18
+ output2 = query2({
19
+ "inputs": {
20
+ "question": question,
21
+ "context": context
22
+ },
23
+ })
24
+ answer = output2["answer"]
25
+ return render_template("index.html", answer=answer)
26
+
27
+ if __name__ == "__main__":
28
+ app.run(debug=True)