clementrof commited on
Commit
bfbcac4
·
verified ·
1 Parent(s): 46f1835

Delete app4.py

Browse files
Files changed (1) hide show
  1. app4.py +0 -194
app4.py DELETED
@@ -1,194 +0,0 @@
1
- import gradio as gr
2
- import os
3
- import time
4
- import requests
5
- import base64
6
-
7
- import pymongo
8
- import certifi
9
-
10
-
11
- token = '5UAYO8UWHNQKT3UUS9H8V360L76MD72DRIUY9QC2'
12
-
13
- # Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
14
-
15
- uri = "mongodb+srv://clementrof:t5fXqwpDQYFpvuCk@cluster0.rl5qhcj.mongodb.net/?retryWrites=true&w=majority"
16
-
17
- # Create a new client and connect to the server
18
- client = pymongo.MongoClient(uri, tlsCAFile=certifi.where())
19
-
20
- # Send a ping to confirm a successful connection
21
- try:
22
- client.admin.command('ping')
23
- print("Pinged your deployment. You successfully connected to MongoDB!")
24
- except Exception as e:
25
- print(e)
26
-
27
- # Access your database
28
- db = client.get_database('camila')
29
- records = db.info
30
-
31
-
32
- #########################################
33
- #########################################
34
-
35
- def LLM_call(message_log):
36
-
37
- serverless_api_id = '4whzcbwuriohqh'
38
- # Define the URL you want to send the request to
39
- url = f"https://api.runpod.ai/v2/{serverless_api_id}/run"
40
-
41
- # Define your custom headers
42
- headers = {
43
- "Authorization": f"Bearer {token}",
44
- "Accept": "application/json",
45
- "Content-Type": "application/json"
46
- }
47
-
48
-
49
-
50
- # Define your data (this could also be a JSON payload)
51
- data = {
52
-
53
- "input": {
54
- "prompt": message_log,
55
- "max_new_tokens": 4500,
56
- "temperature": 0.7,
57
- "top_k": 50,
58
- "top_p": 0.9,
59
- "repetition_penalty": 1.2,
60
- "batch_size": 8,
61
- "stop": ["</s>"]
62
- }
63
- }
64
-
65
-
66
-
67
-
68
- # Send the POST request with headers and data
69
- call = requests.post(url, headers=headers, json=data)
70
- response_data = call.json()
71
- msg_id = response_data['id']
72
- print("Message ID:", msg_id)
73
- output = "Output not available"
74
- # Poll the API until the response is ready
75
- while True:
76
- # Get the status using the message ID
77
- response = requests.get(f"https://api.runpod.ai/v2/{serverless_api_id}/status/{msg_id}", headers=headers)
78
-
79
- if response.status_code == 200:
80
- response_data = response.json()
81
- status = response_data.get('status')
82
-
83
- if status == 'COMPLETED':
84
- # Access the 'output' directly from the response
85
- output = response_data.get('output', 'Output not available')
86
- print("Response content:", output)
87
- break # Exit the loop once the response is ready
88
- elif status == 'FAILED':
89
- error_message = response_data.get('error', 'Unknown error')
90
- print("Request failed. Reason:", error_message)
91
- break # Exit the loop if the request failed
92
- else:
93
- print("Failed to get status. HTTP status code:", response.status_code)
94
-
95
- # Wait for a short time before polling again (e.g., 2 seconds)
96
- time.sleep(2)
97
-
98
- return output
99
-
100
- #########################################
101
- #########################################
102
-
103
- def print_like_dislike(x: gr.LikeData):
104
- print(x.index, x.value, x.liked)
105
-
106
-
107
- # def add_text(history, text):
108
- # history = history + [(text, None)]
109
- # return history, gr.Textbox(value="", interactive=False)
110
-
111
- def add_text(history, text):
112
- history = history + [((text[0], None),)] # Make sure to put the tuple inside a list
113
- return history, gr.Textbox(value="", interactive=False)
114
-
115
-
116
-
117
- def add_file(history, file):
118
- history = history + [((file.name,), None)]
119
- return history
120
-
121
- #########################################
122
- #########################################
123
-
124
- def bot(history):
125
- ID=2
126
- existing_user_doc = records.find_one({'ID': ID})
127
- if existing_user_doc:
128
- message_log = [{"role": "system", "content": f"I want you to act as a woman named Julie. Use colloquial language. Julie is 25 years old, she is very hot physically, she loves watching football, she loves to talk about sex. Julie loves sex and being fucked. She never refuses anything. You'll answer to a man, call him baby. Write very short answers, 15 words."},
129
- ]
130
- messages = existing_user_doc['message']
131
- if len(messages)>5:
132
- messages = messages[-5:]
133
-
134
- message_log.extend(messages)
135
- print (history)
136
- new_message = {"role": "user", "content": history}
137
- message_log.append(new_message)
138
-
139
-
140
- response = LLM_call(message_log)
141
-
142
- else:
143
- new_user_doc = {
144
- 'ID': ID,
145
- 'message': []
146
- }
147
- records.insert_one(new_user_doc)
148
- response = "Hello"
149
-
150
- history[-1][1] = ""
151
- for character in response:
152
- history[-1][1] += character
153
- time.sleep(0.05)
154
- yield history
155
-
156
-
157
- #########################################
158
- #########################################
159
-
160
- with gr.Blocks() as demo:
161
- chatbot = gr.Chatbot(
162
- [],
163
- elem_id="chatbot",
164
- bubble_full_width=False,
165
- avatar_images=(None, (os.path.join(os.path.dirname(__file__), "avatar.jpg"))),
166
- )
167
-
168
- with gr.Row():
169
- txt = gr.Textbox(
170
- scale=4,
171
- show_label=False,
172
- placeholder="Enter text and press enter, or upload an image",
173
- container=False,
174
- )
175
- btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
176
-
177
- txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
178
- bot, chatbot, chatbot, api_name="bot_response"
179
- )
180
- txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
181
- file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
182
- bot, chatbot, chatbot
183
- )
184
-
185
- chatbot.like(print_like_dislike, None, None)
186
-
187
-
188
- demo.queue()
189
- if __name__ == "__main__":
190
- demo.launch()
191
-
192
-
193
-
194
-