suwesh commited on
Commit
2e0f8aa
Β·
verified Β·
1 Parent(s): 21f6c89

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ from transformers import pipeline
4
+ from gtts import gTTS
5
+ import io
6
+ import tempfile
7
+
8
+ """
9
+ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
10
+ """
11
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
12
+
13
+ modelpath = "distilgpt2"
14
+
15
+ pipe = pipeline(
16
+ "text-generation",
17
+ model=modelpath
18
+ )
19
+ messages = [
20
+ {"role": "system", "content": "You are a customer applying for a housing loan in India. Provide dummy details about your application and negotiate the terms."},
21
+ {"role": "user", "content": "Hi!Welcome to Hero Housing Finance!"},
22
+ {"role": "assistant", "content": "Hello, I would like to apply for a loan."},
23
+ ]
24
+ #outputs = pipe(
25
+ # messages,
26
+ # max_new_tokens=256,
27
+ #)
28
+ #print(outputs[0]["generated_text"][-1])
29
+ def continue_conversation(new_message):
30
+ messages.append({"role": "user", "content":new_message})
31
+ outputs = pipe(
32
+ messages,
33
+ max_new_tokens=256,
34
+ )
35
+ response = outputs[0]["generated_text"]
36
+ #print(response)
37
+ messages.append({"role": "user", "content": response})
38
+ return response
39
+ #new_message = "Sure, I can help with that. Could you please provide me with some details about the loan you need?"
40
+ #continue_conversation(new_message)
41
+
42
+ def respond(
43
+ message,
44
+ history: list[tuple[str, str]],
45
+ system_message,
46
+ max_tokens,
47
+ temperature,
48
+ top_p,
49
+ ):
50
+ messages = [{"role": "system", "content": system_message}]
51
+
52
+ for val in history:
53
+ if val[0]:
54
+ messages.append({"role": "user", "content": val[0]})
55
+ if val[1]:
56
+ messages.append({"role": "assistant", "content": val[1]})
57
+
58
+ messages.append({"role": "user", "content": message})
59
+
60
+ response = ""
61
+
62
+ for message in client.chat_completion(
63
+ messages,
64
+ max_tokens=max_tokens,
65
+ stream=True,
66
+ temperature=temperature,
67
+ top_p=top_p,
68
+ ):
69
+ token = message.choices[0].delta.content
70
+
71
+ response += token
72
+ yield response
73
+
74
+
75
+ """
76
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
77
+ """
78
+ demo = gr.ChatInterface(
79
+ respond,
80
+ additional_inputs=[
81
+ gr.Textbox(value="You are a Roleplaying Customer to a Housing Finance and Loans Company. You dont have to say that you are a role playing customer, that makes you seem machine, fake it and try your best to appear as a human.", label="System message"),
82
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
83
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
84
+ gr.Slider(
85
+ minimum=0.1,
86
+ maximum=1.0,
87
+ value=0.95,
88
+ step=0.05,
89
+ label="Top-p (nucleus sampling)",
90
+ ),
91
+ ],
92
+ )
93
+
94
+
95
+ if __name__ == "__main__":
96
+ demo.launch()