akhil2808 commited on
Commit
ba395cf
1 Parent(s): 796f984

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +2 -9
  2. app.py +96 -0
  3. gitattributes +35 -0
  4. requirements.txt +7 -0
README.md CHANGED
@@ -1,13 +1,6 @@
1
  ---
2
- title: MongoDBpixtralOCR
3
- emoji: 🏆
4
- colorFrom: purple
5
- colorTo: green
6
  sdk: gradio
7
  sdk_version: 4.44.0
8
- app_file: app.py
9
- pinned: false
10
- license: apache-2.0
11
  ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: pixtral
3
+ app_file: app.py
 
 
4
  sdk: gradio
5
  sdk_version: 4.44.0
 
 
 
6
  ---
 
 
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import requests
3
+ from io import BytesIO
4
+ from PIL import Image
5
+ import gradio as gr
6
+ from pymongo import MongoClient
7
+ import time
8
+ import uuid
9
+
10
+ # MongoDB setup
11
+ mongo_client = MongoClient("mongodb+srv://atharva2021:[email protected]/")
12
+ db = mongo_client['bajaj']
13
+ collection = db['client']
14
+
15
+ # Function to encode the image to base64
16
+ def encode_image(img):
17
+ buffered = BytesIO()
18
+ img.save(buffered, format="PNG")
19
+ encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
20
+ return encoded_string
21
+
22
+ # Chat function with Pixtral and MongoDB saving
23
+ def chat_with_pixtral(uploaded_file, mrn_number, user_question):
24
+ if uploaded_file is not None and mrn_number.strip() != "":
25
+ base64_img = encode_image(uploaded_file)
26
+
27
+ api = "https://api.hyperbolic.xyz/v1/chat/completions"
28
+ api_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJyZzMyNzAyNEBnbWFpbC5jb20ifQ._frFve-BYZdb0Qo6FIj6xcDcxpY-6QlC2O-ToQxBjkc" # Add your API key here
29
+
30
+ headers = {
31
+ "Content-Type": "application/json",
32
+ "Authorization": f"Bearer {api_key}",
33
+ }
34
+
35
+ payload = {
36
+ "messages": [
37
+ {
38
+ "role": "user",
39
+ "content": [
40
+ {"type": "text", "text": user_question},
41
+ {
42
+ "type": "image_url",
43
+ "image_url": {"url": f"data:image/jpeg;base64,{base64_img}"},
44
+ },
45
+ ],
46
+ }
47
+ ],
48
+ "model": "mistralai/Pixtral-12B-2409",
49
+ "max_tokens": 2048,
50
+ "temperature": 0.7,
51
+ "top_p": 0.9,
52
+ }
53
+
54
+ response = requests.post(api, headers=headers, json=payload)
55
+
56
+ # Process API response
57
+ if response.status_code == 200:
58
+ response_data = response.json()
59
+ if 'choices' in response_data:
60
+ assistant_response = response_data['choices'][0]['message']['content']
61
+ else:
62
+ assistant_response = "Response format is incorrect"
63
+ else:
64
+ assistant_response = f"API request failed: {response.status_code} - {response.text}"
65
+
66
+ # Generate a unique ID for the request
67
+ unique_id = str(uuid.uuid4())
68
+
69
+ # Save the result to MongoDB with the specified format
70
+ document = {
71
+ 'mrn_number': mrn_number,
72
+ 'ocr_result': assistant_response, # This will be the OCR/API result
73
+ 'unique_id': unique_id,
74
+ 'got_mode': "plain texts OCR",
75
+ 'timestamp': time.time()
76
+ }
77
+
78
+ collection.insert_one(document) # Insert the document into MongoDB
79
+
80
+ return assistant_response
81
+ return "Upload your image, enter MRN number, and enter your question."
82
+
83
+ # Gradio GUI
84
+ iface = gr.Interface(
85
+ fn=chat_with_pixtral,
86
+ inputs=[
87
+ gr.Image(type="pil", label="Upload Your Image"),
88
+ gr.Textbox(label="Enter MRN Number"),
89
+ gr.Textbox(label="Please enter your question")
90
+ ],
91
+ outputs="text",
92
+ title="Pixtral Image Chat",
93
+ description="Upload your Image, enter MRN number, and get insights out of the Image"
94
+ )
95
+
96
+ iface.launch(share=True)
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ accelerate
2
+ diffusers
3
+ invisible_watermark
4
+ torch
5
+ transformers
6
+ xformers
7
+ pymongo