lakshmivairamani commited on
Commit
c9f5de7
·
verified ·
1 Parent(s): f9cdb92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -62
app.py CHANGED
@@ -1,63 +1,73 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- 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
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
  import gradio as gr
3
+ import numpy as np
4
+ from pandasai import SmartDataframe
5
+ from pandasai.llm import OpenAI
6
+ from PIL import Image
7
+ import base64
8
+ from io import BytesIO
9
+
10
+ def ask(query, file, api_token):
11
+ data = pd.read_csv(file, index_col=0)
12
+ llm = OpenAI(api_token=api_token)
13
+ df = SmartDataframe(data, config={"llm": llm})
14
+ result = df.chat(query)
15
+ if isinstance(result, str) and result.endswith('.png'):
16
+ # Open the image file
17
+ img = Image.open('exports/charts/temp_chart.png')
18
+
19
+ # Convert the PIL Image to a base64 encoded string
20
+ buffered = BytesIO()
21
+ img.save(buffered, format="PNG")
22
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
23
+
24
+ # Construct the HTML string to display the image
25
+ html = f"<img src='data:image/png;base64,{img_str}' alt='Generated Image' style='width:100%;'>"
26
+ return html
27
+ elif isinstance(result, pd.DataFrame):
28
+ html_table = result.to_html()
29
+ return html_table
30
+ else:
31
+ return result
32
+
33
+ def on_file_upload(file, api_token):
34
+ data = pd.read_csv(file, index_col=0)
35
+ llm = OpenAI(api_token=api_token)
36
+ df = SmartDataframe(data, config={"llm": llm})
37
+ result = df.chat('What are the name of my columns')
38
+ return result
39
+
40
+ with gr.Blocks() as demo:
41
+ headers = gr.Markdown("<div style='text-align: center;'><h1> This is a project from Chasers using Pandas AI</h1></div>")
42
+
43
+ with gr.Row():
44
+ # First column for the first image
45
+ with gr.Column():
46
+ gr.HTML("""
47
+ <img src="https://d1b66s7evqera.cloudfront.net/f8d9b474-d1d2-4931-b38d-c5f918b8dc82/images/Chasers320x132.png?61554cbec993ea37a9d52399a0a0d3cd" width="320">
48
+ """)
49
+
50
+ # Second column for the clickable image
51
+ with gr.Column():
52
+ gr.HTML("""
53
+ <a href='https://pandas-ai.com/' target='_blank'>
54
+ <img src="https://framerusercontent.com/images/tDg1U6HZYYK3azrDbdVXLhPkOlk.png" width="150" style="opacity: 0.3";>
55
+ </a>
56
+ """)
57
+
58
+ api_token_input = gr.Textbox(lines=1, label="Enter your API token")
59
+ file_input = gr.File()
60
+ upload_button = gr.Button("Process Uploaded File")
61
+ upload_message = gr.Label("")
62
+ upload_button.click(on_file_upload, inputs=[file_input, api_token_input], outputs=[upload_message])
63
+ query_input = gr.Textbox(lines=2, label="Use exactly the name of the columns in your querys")
64
+ output_html = gr.HTML()
65
+
66
+
67
+
68
+ # Button to trigger the ask function
69
+ submit_button = gr.Button("Submit")
70
+ submit_button.click(ask, inputs=[query_input, file_input, api_token_input], outputs=[output_html])
71
+
72
+ demo.launch()
73
+