fahmifauzi commited on
Commit
4d08477
·
verified ·
1 Parent(s): 4b70b51

initial commit

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Load HF API key
2
+ import os
3
+ import io
4
+ from IPython.display import Image, display, HTML
5
+ from PIL import Image
6
+ import base64
7
+ from dotenv import load_dotenv, find_dotenv
8
+ _ = load_dotenv(find_dotenv())
9
+ hf_api_key = os.environ['HF_TOKEN']
10
+
11
+
12
+ #Text-generation endpoint for the mistral model
13
+ import requests, json
14
+
15
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
16
+ headers = {"Authorization": f"Bearer {hf_api_key}",
17
+ "Content-Type": "application/json"}
18
+
19
+ def query(inputs, parameters=None, ENDPOINT_URL=API_URL):
20
+ data = {"inputs": inputs}
21
+ if parameters is not None:
22
+ data.update({"parameters": parameters})
23
+ response = requests.request("POST", ENDPOINT_URL, headers=headers, data=json.dumps(data))
24
+ return json.loads(response.content.decode("utf-8"))
25
+
26
+
27
+ #Defining text generation function
28
+ def generate_text(inputs):
29
+ output = query(inputs)
30
+ return output[0]['generated_text']
31
+
32
+
33
+ #Gradio Interface
34
+ import gradio as gr
35
+
36
+ gr.close_all()
37
+ demo = gr.Interface(fn=generate_text,
38
+ inputs=[gr.Textbox(label="Prompt", lines=5)],
39
+ outputs=[gr.Textbox(label="Generated Text", lines=5)],
40
+ title="Text Generator",
41
+ description="Text generation using `mistralai/Mistral-7B-Instruct-v0.1` model."
42
+ )
43
+
44
+ demo.launch(share=True)